Chapter VII · Objects

Object Destructuring

Object destructuring unpacks properties from objects directly into distinct local variables.

01 How It Works

Think of unpacking designated items straight out of a shipment box.

Property names match local variable identifiers on the assignment left-hand side.

example.js
const user = { name: "Charlie", age: 30 };
const { name, age } = user;
console.log(name); // "Charlie"
  1. 1. Target a source object.

  2. 2. Wrap desired property keys inside curly braces on the left side.

  3. 3. Use extracted variables directly.

02 Practical Example

Here is how you might see this concept applied in real-world code:

practical.js
const settings = { theme: "dark", lang: "en" };
const { theme } = settings;

Key Takeaway

Destructuring provides a clean shorthand for extracting object properties.

Related Concepts