Define and call a function
#Parameters use name: type; the return type follows ). Call with parentheses. An arrow is another function form; void means no useful return value.
Fragment
Function declaration and call.
Reference
Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.
TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.
TypeScript docs More on functions ↗Classes ↗
Parameters use name: type; the return type follows ). Call with parentheses. An arrow is another function form; void means no useful return value.
Fragment
Function declaration and call.
Node evaluates top-level code; main is a chosen name, not a required entry point. async returns a Promise. void discards that Promise value; it does not catch rejection.
Fragment
Complete Node program.
class defines a runtime type; new constructs it. this addresses the instance. private restricts TypeScript access. User-defined class and method names are application APIs, not TypeScript keywords.
Fragment
Complete custom declaration and use.
new calls a class constructor to initialize an instance. this refers to that instance. A type alias or interface has no runtime constructor; an object literal can create a value with that shape.
Fragment
Fragment: imports and type/function declarations at file scope; usage statements inside a function.
Functions are values. An arrow can capture variables from its surrounding scope and uses lexical this. Capturing a mutable binding does not take a frozen snapshot of its value.
Fragment
Fragment: imports and type/function declarations at file scope; usage statements inside a function.
Default parameters apply when the argument is omitted or undefined. A rest parameter collects remaining arguments into an array. Spread expands an iterable at a call; it is not the same operation as a rest declaration.
Fragment
Fragment: imports and type/function declarations at file scope; usage statements inside a function.