Destructuring objects

suggest change

Destructuring extracts object properties into bindings:

const person = {
  name: 'Bob',
  age: 25,
};

const { name, age } = person;

Rename a property by placing the local binding name after a colon:

const { name: firstName } = person;
console.log(firstName); // 'Bob'

A default initializer is used only when the property value is undefined; it is not used for null, false, or other falsy values:

const { phone = '123-456-789' } = person;
console.log(phone); // '123-456-789'

Renaming and defaults can be combined:

const { phone: contactNumber = '123-456-789' } = person;

Nested patterns and rest properties can extract more complex shapes:

const response = {
  user: { id: 42, displayName: 'Bob' },
  cached: true,
};

const {
  user: { id, displayName },
  ...metadata
} = response;

Destructuring null or undefined throws a TypeError; validate or provide a fallback before destructuring an optional value.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents