🟑 MediumClosures & ScopeπŸ’» Output Question

Nested object destructuring with defaults

πŸ’‘

Hint

You can destructure a nested path AND keep the parent reference in the same destructuring. Defaults only apply when the value is undefined.

What does this output?

const user = {
  name: 'Alice',
  address: {
    city: 'Paris',
  }
};

const {
  name,
  age = 25,
  address: { city, country = 'France' },
  address
} = user;

console.log(name);
console.log(age);
console.log(city);
console.log(country);
console.log(address);

Correct Output

Alice
25
Paris
France
{ city: 'Paris' }

Why this output?

Explanation: name = "Alice" from object. age = 25 (default, not in object). address: destructures into city and country with default. address = the address object itself (can destructure and also capture).

Key Insight: You can destructure a nested path AND keep the parent reference in the same destructuring. Defaults only apply when the value is undefined.

More Closures & Scope Output Questions

🟒 EasyClassic var in loop closureβ†’πŸŸ’ Easylet in loop closure (fix)β†’πŸŸ‘ MediumClosure counterβ†’πŸŸ‘ MediumIIFE closureβ†’

Practice predicting output live β†’

66 output questions with instant feedback

πŸ’» Try Output Quiz