Array Destructuring
Array destructuring assigns array elements to variables by position. Skip elements with commas, collect the rest with ....
Array destructuring assigns array elements to variables by position. Skip elements with commas, collect the rest with ....
const [first, second, ...rest] = [1, 2, 3, 4, 5];
// first=1, second=2, rest=[3,4,5]
const [, , third] = [10, 20, 30]; // skip first two
const [a = 5, b = 7] = [1]; // defaults: a=1, b=7
const [r, g, b2] = "rgb(255,0,128)".match(/\d+/g).map(Number);
Array destructuring is positional — use object destructuring when order does not matter.
More in JavaScript