Table of Contents
What Array Destructuring Does
Unpacks values into variables: each variable gets the value at the matching position.
Uses square brackets on the left side: the brackets look like an array, but they describe the variables you want to create.
Matches by position: the first variable gets the first item, the second variable gets the second item, and so on.
Leaves the original array alone: nothing in the array is changed.
let colors = ["red", "green", "blue"]; let [first, second, third] = colors; console.log(first); // "red" console.log(second); // "green" console.log(third); // "blue"
The Old Way vs the New Way
let colors = ["red", "green", "blue"]; let first = colors[0]; let second = colors[1]; let third = colors[2];With destructuring: one line does the same job.
let [first, second, third] = colors;The result is the same: the second version is shorter and easier to read.
Syntax
let [variable1, variable2, variable3] = array;
Left side: the names of the variables you want to create.
Right side: the array you are unpacking.
It works with const too: which is common when the values will not change.
const [x, y] = [10, 20]; console.log(x + y); // 30
Skipping Items
Leave a gap with a comma: the position is skipped, and no variable is created for it.let scores = [90, 85, 70, 60]; let [top, , third] = scores; console.log(top); // 90 console.log(third); // 70Skip several items at the start: use more commas.
let [, , , last] = scores; console.log(last); // 60
Default Values
let [a, b, c = "default"] = [1, 2]; console.log(a); // 1 console.log(b); // 2 console.log(c); // "default"Without a default: the variable becomes undefined.
let [p, q, r] = [1, 2]; console.log(r); // undefinedDefaults do not apply to null: only to undefined.
let [m = "fallback"] = [null]; console.log(m); // null
The Rest Pattern
Collect the remaining items: use three dots before the last variable.let [first, ...others] = [1, 2, 3, 4, 5]; console.log(first); // 1 console.log(others); // [2, 3, 4, 5]
The rest variable is always an array: and it must come last.
It can be empty: when there are no items left.
let [only, ...rest] = [1]; console.log(rest); // []
Swapping Variables
No temporary variable needed: destructuring can swap two values in one line.let a = 1; let b = 2; [a, b] = [b, a]; console.log(a); // 2 console.log(b); // 1Before destructuring: you needed a third variable to hold one value.
let temp = a; a = b; b = temp;A semicolon before the line helps: if the previous line has no semicolon, JavaScript may read the brackets as part of it.
Practical Examples
Returning several values from a function:
function getMinMax(numbers) {
return [Math.min(...numbers), Math.max(...numbers)];
}
let [min, max] = getMinMax([4, 9, 2, 7]);
console.log(min); // 2
console.log(max); // 9
Using it with split():
let date = "2026-10-31";
let [year, month, day] = date.split("-");
console.log(year); // "2026"
console.log(month); // "10"
console.log(day); // "31"
Looping through pairs with Object.entries():
let user = { name: "Vi", age: 22 };
for (let [key, value] of Object.entries(user)) {
console.log(`${key}: ${value}`);
}
// name: Vi
// age: 22
Unpacking function parameters:
function printPoint([x, y]) {
console.log(`x is ${x} and y is ${y}`);
}
printPoint([5, 8]); // "x is 5 and y is 8"
The useState pattern in React:
// const [count, setCount] = useState(0);
Nested Destructuring
Unpack arrays inside arrays: use brackets inside brackets.let data = [1, [2, 3], 4]; let [one, [two, three], four] = data; console.log(two); // 2 console.log(three); // 3Keep it readable: deeply nested patterns can be hard to follow, so use them sparingly.
Things to Remember
Position matters: the variable names do not need to match anything in the array.
It works with any iterable: including strings and sets, not only arrays.
let [h, i] = "hi"; console.log(h); // "h" console.log(i); // "i"Destructuring null or undefined throws an error: so make sure the value exists first.
let [x] = undefined; // TypeErrorA safe fallback: use an empty array.
let [x = 0] = maybeArray || [];The unpacked values are copies: for objects inside the array, the variable points to the same object.
Array vs Object Destructuring
| Feature | Array Destructuring | Object Destructuring |
| Brackets | Square brackets [ ] | Curly braces { } |
| Matches by | Position | Property name |
| Variable names | Any name you like | Must match the property, unless renamed |
| Best for | Lists and pairs | Records with named fields |
let [first] = ["a", "b"]; // by position
let { name } = { name: "Vi", age: 22 }; // by name
Common Mistakes
Using curly braces for an array: let { a, b } = [1, 2] will not work as you expect.
Forgetting that the rest item must come last: [...rest, last] is a syntax error.
Destructuring a value that might not exist: this throws an error, so add a fallback.
Expecting names to matter: only the position decides which value a variable gets.
Skipping without the comma: each skipped position needs its own comma.
Conclusion
Frequently Asked Questions (FAQs)
2.What happens if the array has fewer items than variables?No. It only reads the values and copies them into variables.
3.Can I destructure something that is not an array?The extra variables become undefined, unless you have set default values.
Yes, any iterable works, including strings, sets and maps. For objects, use curly braces instead.
0 Comments