jsrosetta

Functions

Destructuring

Object destructuring in Node.js compared to struct pattern destructuring in Rust, tuple destructuring in Swift, record patterns in Java, and multiple assignment in Go.

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 destructures an object directly into multiple variables with { key, value } = obj. Rust has real struct destructuring through pattern matching (let Obj { key, value } = obj) — the closest match to JS. Swift doesn't destructure a struct by field name directly, but it does destructure tuples (let (key, value) = (...)). Java has had record patterns since version 21, letting you destructure a record right inside instanceof. Go has no destructuring syntax for structs, but you get the same result with multiple assignment or a function that returns multiple values.

Pulling multiple values out of an object

const obj = { key: 'foo', value: 'bar' };
 
const { key, value } = obj;
console.log(key, value); // foo bar

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