Sub topic 1
Unpacking Arrays by Position
Array destructuring lets you pull values out of an array and into separate variables in one statement. It matches values by position. The first variable gets the first element, the second variable gets the second, and so on.
Prerequisites
This chapter assumes you can already:
- Declare variables with
letandconst - Create arrays and read elements by index, such as
arr[0] - Write and call functions, including arrow functions
The basic pattern
Without destructuring, you read each element by index:
const point = [10, 20];
const x = point[0];
const y = point[1];
console.log(x, y); // 10 20With destructuring, you write a pattern on the left side of = that has the same shape as the array:
const point = [10, 20];
const [x, y] = point;
console.log(x, y); // 10 20The brackets on the left side do not create an array. They form a pattern that says "take element 0 into x and element 1 into y."
Fewer or more variables than elements
The pattern does not have to match the array's length.
- If the array has extra elements, they are ignored.
- If the pattern has extra variables, those variables become
undefined.
const [a, b] = [1, 2, 3, 4];
console.log(a, b); // 1 2 (3 and 4 are ignored)
const [p, q, r] = [1, 2];
console.log(r); // undefined (no element at index 2)Neither case throws an error. The second case is a common source of quiet bugs: a missing value shows up later as undefined instead of failing right away. A later chapter shows how default values fix this.
Skipping elements
To skip a position, leave its slot empty and keep the comma:
const colors = ['red', 'green', 'blue'];
const [primary, , tertiary] = colors;
console.log(primary); // 'red'
console.log(tertiary); // 'blue'Each comma moves to the next index. [, , third] takes only index 2. Use this sparingly. Several empty slots in a row are hard to read, and plain indexing such as colors[4] may be clearer.
Destructuring return values
Array destructuring works well when a function returns several values in a fixed order:
function minMax(numbers) {
return [Math.min(...numbers), Math.max(...numbers)];
}
const [low, high] = minMax([4, 9, 1, 7]);
console.log(low, high); // 1 9The ...numbers in this example is spread syntax, which gets its own chapter. React's useState hook uses the same return-a-pair idea: const [count, setCount] = useState(0).
Common errors
| Mistake | What happens |
|---|---|
| Getting the order wrong, as in `const [y, x] = point` | The values land in the wrong variables with no warning |
| Destructuring a non-iterable, as in `const [a] = 42` | `TypeError: 42 is not iterable` |
| Expecting a missing element to throw | You get `undefined` instead |
Exercise
What do first, second, and fourth hold after const [first, , second, fourth] = ['w', 'x', 'y'];?
Show answer
first is 'w'. The empty slot skips 'x', so second is 'y'. The array has no element at index 3, so fourth is undefined.
Exercise
Write one line that takes the result of '2026-11-24'.split('-') and stores the year and day in variables year and day, skipping the month.
Show answer
const [year, , day] = '2026-11-24'.split('-'); gives year the value '2026' and day the value '24'.