SyntaxStudy
Sign Up
JavaScript Beginner 3 min read

Array Destructuring

Array Destructuring

Array destructuring assigns array elements to variables by position. Skip elements with commas, collect the rest with ....

Example
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);
Pro Tip

Array destructuring is positional — use object destructuring when order does not matter.