Skip to main content
TOPIC 2 OF 8 · 2 MIN READ

Multi-Level Destructuring

Multi-level destructuring allows unpacking values from nested objects or arrays in a single statement.

Key concepts

Multi-Level Destructuring

Allows unpacking values from nested objects or arrays in a single statement.

Examples

Nested Object Destructuring:

const user = {
  name: 'Alice',
  address: {
    city: 'New York',
    location: { latitude: 40.7128, longitude: -74.0060 }
  }
};

const {
  address: {
    city,
    location: { latitude, longitude }
  }
} = user;

console.log(city); // New York
console.log(latitude, longitude); // 40.7128, -74.0060

Nested Array Destructuring:

const numbers = [1, [2, 3], [4, [5, 6]]];
const [first, [second, third], [fourth, [fifth, sixth]]] = numbers;

console.log(first); // 1
console.log(second, third); // 2, 3
console.log(fourth, fifth, sixth); // 4, 5, 6

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus