Skip to content

Object Destructuring

Destructuring copies properties out of an object into variables in one statement. The left-hand side of the assignment looks like an object literal, and each name in it picks out the property of the same name. It replaces a run of const x = obj.x; lines, and it is at its most useful in function parameters, where it documents which properties a function reads.

const reading = { station: 'Heathrow', temp: 9.4, wind: 18, unit: 'C' };
const { station, temp } = reading;
console.log(station, temp);

This prints:

Heathrow 9.4

Only the properties named are taken, and the order does not matter, because they are matched by name.

A colon after the property name gives the variable a different name, and = gives a default value. The default is used only when the property is missing or undefined, not when it is null:

const reading = { station: 'Heathrow', temp: 9.4, unit: 'C', rain: null };
const { temp: celsius, humidity = 'n/a', rain = 0 } = reading;
console.log(celsius, humidity, rain);

This prints:

9.4 n/a null

temp is read into a variable called celsius. humidity does not exist, so it takes its default, while rain exists with the value null, so the default is not used.

A pattern can reach into a nested object by nesting another pattern. A final ...name collects every property that was not picked out into a new object:

const reading = {
station: 'Heathrow',
location: { lat: 51.48, lon: -0.45 },
temp: 9.4,
wind: 18,
};
const { location: { lat, lon }, ...rest } = reading;
console.log(lat, lon);
console.log(rest);

This prints:

51.48 -0.45
{ station: 'Heathrow', temp: 9.4, wind: 18 }

location here is only a path to follow, so no variable called location is created. The rest object is a shallow copy, which makes this a simple way to copy an object without some of its properties.

A function that takes an options object can destructure it in its parameter list, with defaults for the optional properties. Without destructuring, the same function reads each property by hand:

function describeOld(reading) {
const station = reading.station;
const temp = reading.temp;
const unit = reading.unit !== undefined ? reading.unit : 'C';
const wind = reading.wind !== undefined ? reading.wind : 0;
return station + ': ' + temp + '°' + unit + ', wind ' + wind + ' mph';
}
console.log(describeOld({ station: 'Heathrow', temp: 9.4, wind: 18 }));

This prints:

Heathrow: 9.4°C, wind 18 mph

With destructuring, the parameter list states the properties and their defaults in one place, and callers can pass them in any order:

function describe({ station, temp, unit = 'C', wind = 0 }) {
return `${station}: ${temp}°${unit}, wind ${wind} mph`;
}
console.log(describe({ station: 'Heathrow', temp: 9.4, wind: 18 }));
console.log(describe({ temp: 48, unit: 'F', station: 'Aberdeen' }));

This prints:

Heathrow: 9.4°C, wind 18 mph
Aberdeen: 48°F, wind 0 mph

Destructuring undefined throws a TypeError, so a function whose argument is optional needs = {} after the pattern, which supplies an empty object when nothing is passed:

function strict({ unit = 'C' }) {
return unit;
}
function relaxed({ unit = 'C' } = {}) {
return unit;
}
try {
strict();
} catch (err) {
console.log(err.name);
}
console.log(relaxed());

This prints:

TypeError
C

Destructuring has a counterpart for building objects. When a variable has the same name as the property it should fill, the literal can name it once:

const station = 'Heathrow';
const temp = 9.4;
const summary = { station, temp, checked: true };
console.log(summary);

This prints:

{ station: 'Heathrow', temp: 9.4, checked: true }

To destructure into variables that already exist, leave out const or let and wrap the whole statement in parentheses. Without them, JavaScript reads the opening brace as the start of a block:

let high = 0;
let low = 0;
({ high, low } = { high: 14, low: 6 });
console.log(high, low);

This prints:

14 6

Steve Griffith covers this topic in his video Why JS Developers Love Destructuring.