jsrosetta

Functions

IIFE

IIFEs in Node.js compared to immediately invoked anonymous functions/closures in 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.

An IIFE (Immediately Invoked Function Expression) is a function defined and called right away, usually to create its own scope. Go, Rust, and Swift have no special syntax for this — you just define an anonymous function or closure and call it immediately. Java is harder: a lambda expression needs a concrete functional-interface type, so it can't be called directly — you have to cast it first. Nobody writes real Java like this; a plain { ... } block or a private method is the usual way to create a scope instead of faking an IIFE.

Defining and calling a function immediately

(function (name) {
  console.log('hello', name);
})('bob'); // hello bob

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