Skip to main content

How to Destructure an Object in JavaScript

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Reading several properties of an object usually takes one line each: const name = user.name;, const city = user.city;. Destructuring does it in one line, const { name, city } = user;, and creates a variable for each property you list.

Syntax​

Syntax
const { <property>, <property> = <default>, <property>: <newName> } = <object>;
PartWhat it does
{ <property> }Creates a variable named after the property, holding its value.
<property> = <default>Uses <default> when the object has no such property.
<property>: <newName>Puts the property's value in a variable with another name.
<object>The object to read from.

MDN's destructuring reference: "The destructuring syntax is a JavaScript syntax that makes it possible to unpack values from arrays, or properties from objects, into distinct variables."

The Example​

The example takes a user object, pulls out its name and city in one line, and asks for a country the object doesn't have, with a default.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which destructures name and city from a user object with name Ada, city London and age 36, prints both, then destructures country with the default unknown and prints it.

main.js
const user = { name: 'Ada', city: 'London', age: 36 };

const { name, city } = user;
console.log(name);
console.log(city);

const { country = 'unknown' } = user;
console.log(country);
  • const { name, city } = user; creates two variables, name and city, from the properties with those names. age is left alone.
  • const { country = 'unknown' } = user; asks for country. The object has none, so the default applies.

Step 2: Run It​

Terminal
node main.js
Output
Ada
London
unknown

A zsh terminal where node main.js prints Ada, London and unknown.

The command is the same in zsh, bash and PowerShell.

Renaming While You Destructure​

When the variable needs a different name than the property, put the new name after a colon:

Rename
const { name: fullName } = user;
console.log(fullName); // Ada

Destructuring works on any expression that gives an object, so you can keep only the part you need: const { hostname } = new URL('https://example.com/books'); sets hostname to 'example.com'.

Common Mistakes​

MistakeWhat happensDo this instead
Destructuring undefined or nullTypeError: Cannot destructure property 'name' of 'undefined' as it is undefined.Check that the object exists first
Misspelling a property, as in { nmae }The variable is undefined, with no errorMatch the property name exactly
Expecting { name: fullName } to create nameOnly fullName is createdUse the new name after the colon

Troubleshooting​

SymptomLikely causeFix
TypeError: Cannot destructure property '…' of '…' as it is undefined.The value on the right is undefinedFind out why it's missing, or write = {} as a fallback: const { name } = user ?? {};
A destructured variable is undefinedThe object has no property with that namePrint the object and compare the names

FAQ​

Can I destructure arrays too?​

Yes, with square brackets: const [first, second] = ['red', 'green']; takes items by position instead of by name.

Does destructuring change the object?​

No. It copies values into new variables; the object stays as it was.

Conclusion​

const { a, b } = object; creates variables from properties in one line. Add = default for properties that might be missing and : newName to rename.

Additional Resources​