Nested Array Destructuring
What You'll Learn: You'll learn how to extract values from arrays that live inside other arrays using nested bracket patterns in destructuring syntax.
The Core Idea
Just as arrays can contain other arrays, your destructuring patterns can contain nested brackets to reach into those inner arrays. Think of it like opening a box that contains another box—you need two sets of hands (or in this case, two sets of brackets) to get to the innermost items.
When you have an array structure like [[1, 2], [3, 4]], you can destructure it in a single line by mirroring that structure with nested brackets: [[a, b], [c, d]].
How It Works
// A simple nested array
const colors = [['red', 'green'], ['blue', 'yellow']];
// Extract values with nested destructuring
const [[primary1, primary2], [secondary1, secondary2]] = colors;
console.log(primary1); // 'red'
console.log(secondary1); // 'blue'
You can mix and match techniques you've already learned:
// Skip elements in nested arrays
const matrix = [[1, 2, 3], [4, 5, 6]];
const [[, , third], [first]] = matrix;
console.log(third); // 3
console.log(first); // 4
// Use defaults in nested structures
const data = [[10], []];
const [[a = 0], [b = 99]] = data;
console.log(a); // 10
console.log(b); // 99 (default applied)
// Use rest operator in nested context
const coords = [[1, 2, 3, 4], [5, 6]];
const [[x, y, ...restCoords], point2] = coords;
console.log(restCoords); // [3, 4]
Key Takeaway: Nested array destructuring lets you extract values from multi-dimensional arrays by mirroring their structure with nested bracket patterns, making deeply nested data easy to access in a single, readable statement.