Course contentsShow
← JavaScript
Lesson 927 of 2,72321. DestructuringFree lesson

Basic Object Destructuring Syntax

Extract properties from objects into variables using curly braces and property names.

Basic Object Destructuring Syntax

What you'll learn: How to extract properties from objects into variables using a concise, readable syntax.

The Core Idea

Just as you can destructure arrays, JavaScript lets you "unpack" properties from objects directly into variables. Instead of repeatedly writing object.propertyName, you use curly braces {} with the property names you want to extract.

Plain-English Explanation

Imagine you have a box (object) with labeled compartments (properties). Instead of opening the box and retrieving each item one by one with box.item1, box.item2, object destructuring lets you say "give me item1 and item2" all at once, creating separate variables for each.

The Syntax

const person = {
  firstName: 'Alice',
  age: 30,
  city: 'Seattle'
};

// Old way: accessing properties individually
const firstName = person.firstName;
const age = person.age;

// Object destructuring: extract multiple properties at once
const { firstName, age } = person;

console.log(firstName); // 'Alice'
console.log(age);       // 30

Notice the curly braces on the left side of the assignment. The variable names inside the braces must match the property names in the object. JavaScript looks for those properties and creates variables with the same names.

Important Details

  • The variable names must match the object's property keys
  • Order doesn't matter: { age, firstName } works identically
  • You don't need to extract all properties—just the ones you need
  • Unextracted properties remain in the original object
const user = { username: 'bob42', email: 'bob@example.com', role: 'admin' };

const { username, role } = user;
// email wasn't destructured, but that's fine

Key Takeaway: Object destructuring uses curly braces and matching property names to extract values from objects into standalone variables, making your code cleaner and more readable.