Photo by chansu shin on Unsplash

On this page

This chapter follows Functions in JavaScript.

Array destructuring

Position maps to binding. Skip elements with holes, swap without a temp, and apply defaults when the value is undefined.

array-destructure.js
const numbers = [10, 20, 30];
const [x, y, z] = numbers;
const [a = 100, b = 200] = [50];
// a === 50, b === 200
const [first, second] = [1, 2];

Object destructuring

Property names match keys. Renaming uses key: localName. Defaults apply when the property is missing or undefined.

object-destructure.js
const person = { name: "Alice", age: 25 };
const { name, age } = person;
const { city = "Default City" } = person;
// city === "Default City"
const user = { name: "Bob", address: { city: "Berlin", postalCode: 10115 } };
const {
address: { city, postalCode },
} = user;

Aliases and rest in patterns

Rest in objects collects own enumerable keys not already picked; rest in arrays collects trailing elements.

rest-pattern.js
const { name, ...otherDetails } = person;
// otherDetails === { age: 25 }
const [head, ...tail] = [1, 2, 3, 4];
// head === 1, tail === [2, 3, 4]
const { firstName: first, lastName: last } = {
firstName: "Tom",
lastName: "Smith",
};

Function parameters

Destructuring in parameter lists documents the expected shape and combines with defaults.

param-destructure.js
function greetUser({ name, age }) {
console.log(`Hello, ${name}, you are ${age} years old.`);
}
greetUser({ name: "Tom", age: 28 });
const sumPair = ([a, b]) => a + b;
sumPair([3, 4]); // 7

Class constructors often take a single options object and destructure in the parameter list the same way.

Spread in literals and calls

Spread (...) expands iterables in array literals and argument lists, and copies enumerable own properties into object literals (later keys win).

spread.js
const base = [1, 2];
const extended = [...base, 3, 4];
function maxOf(a, b, c) {
return Math.max(a, b, c);
}
maxOf(...extended);
const defaults = { mode: "prod", retries: 3 };
const config = { ...defaults, retries: 5, debug: true };
// { mode: "prod", retries: 5, debug: true }

Spread is shallow—nested objects are shared by reference unless you clone deeper yourself.

Destructuring in loops

for...of pairs naturally with array or object destructuring when iterating collections of records.

loop-destructure.js
const people = [
{ name: "John", age: 30 },
{ name: "Jane", age: 25 },
];
for (const { name, age } of people) {
console.log(name, age);
}

Common pitfalls

Destructuring undefined or null throws. Optional chaining applies to the path before destructuring, not inside a pattern without a default object.

pitfalls.js
const row = undefined;
// const { id } = row; // TypeError
const { id } = row ?? {};

Default values in arrays do not run for missing slots that are explicitly undefined from a sparse array—behavior matches undefined at that index.

Next: Error handling in JavaScript.

Search articles

Type a word to search published articles.

Open the search page