Hint
You can destructure a nested path AND keep the parent reference in the same destructuring. Defaults only apply when the value is undefined.
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);Alice
25
Paris
France
{ city: 'Paris' }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.