jsrosetta

Functions

Functions

How Node.js declares functions and parameter types compared to the explicit function signatures of Go, Rust, Swift, and Java.

Minimum versions
Node.js ≥ 12.20Go ≥ 1.0Rust ≥ 1.58Swift ≥ 2.0Java ≥ 25
Verified on
Node.js 24.12.0Go 1.27.1Rust 1.98.1Swift 6.2.4Java 25.0.4.1

Node.js code is an ES module: save it as .mjs or set "type": "module" in package.json.

Node.js doesn't require you to declare types for parameters or return values — JavaScript is dynamically typed, so types are only known and checked at runtime, not inferred ahead of time. Go is the opposite: every parameter and return value needs an explicit type, and in exchange type errors are caught at compile time instead of at runtime. Rust and Swift also require explicit types, declaring the return type with -> like Go; Java puts the return type before the function name, C/C++ style.

Declaring a function

function add(a, b) {
  return a + b
}
 
const result = add(2, 3)
console.log(result) // 5

Reference: github.com/miguelmota/golang-for-nodejs-developers#functions