Nested Destructuring in Parameters
You'll learn how to extract values from deeply nested objects or arrays directly in a function's parameter list.
The Core Idea
Just as you can destructure nested structures in regular variable assignments, you can do the same right in your function signatures. This lets you reach into multi-level objects or arrays and pull out exactly what you need—without writing extra lines inside the function body.
Think of it like giving someone specific directions to find something in a filing cabinet: "Open the second drawer, grab the folder labeled 'Reports,' and pull out the document titled 'Summary.'" You're telling the function exactly where to dig.
How It Works
When a function receives a nested object or array, you can unpack it layer by layer:
function displayUser({ name, address: { city, zip } }) {
console.log(`${name} lives in ${city}, ${zip}`);
}
const user = {
name: 'Alice',
address: { city: 'Boston', zip: '02101' }
};
displayUser(user);
// "Alice lives in Boston, 02101"
Notice how address: { city, zip } drills into the nested address object. You're destructuring on two levels at once—no intermediate variables needed.
You can do the same with arrays:
function showCoords([[x, y], label]) {
console.log(`${label}: (${x}, ${y})`);
}
showCoords([[10, 20], 'Point A']);
// "Point A: (10, 20)"
The first parameter destructures the outer array, and [x, y] destructures the first inner array.
When to Use It
This shines when functions consistently receive deeply nested data—like API responses or configuration objects. It makes your function signature self-documenting: readers see exactly which nested properties matter.
Key Takeaway: Nested destructuring in parameters lets you extract deeply nested values directly in the function signature, making your code cleaner and more declarative.