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

Destructuring from Nested Objects

Extract values from deeply nested object structures in a single statement.

Destructuring from Nested Objects

You'll learn how to extract values from objects nested inside other objects using a single, clean destructuring statement.

When you have objects inside objects (nested structures), you can "drill down" through multiple levels in one line instead of writing chains like user.address.city. This makes your code cleaner when working with deeply structured data.

The Core Idea

You mirror the nested structure in your destructuring pattern. If your object has properties that are themselves objects, you destructure "inward" by adding more curly braces at each level.

const user = {
  name: 'Alice',
  location: {
    city: 'Portland',
    state: 'OR'
  }
};

// Extract city from the nested location object
const { location: { city } } = user;
console.log(city); // 'Portland'

Notice how location: { city } mirrors the object's structure. You're saying "go into location, then get city."

Multiple Nested Values

You can extract several values at once:

const { location: { city, state } } = user;
console.log(city);  // 'Portland'
console.log(state); // 'OR'

Combining with Renaming and Defaults

You can rename extracted values and provide defaults even when destructuring nested properties:

const profile = {
  user: {
    name: 'Bob'
  }
};

const { user: { name, age = 30 } } = profile;
console.log(name); // 'Bob'
console.log(age);  // 30 (default value)

Important Note

Be careful: if an intermediate property doesn't exist, you'll get an error. The path must exist or you need to provide defaults at each level.

Key Takeaway: Nested object destructuring lets you reach deep into object structures using a pattern that mirrors the original shape, extracting exactly what you need in one concise statement.