Array destructuring
Pulls values by position.
Destructuring pulls values out of arrays or objects into named variables with compact syntax.
const colors = ["red", "green", "blue"]; const [primary, secondary] = colors; console.log(primary, secondary);
Arrays & Collections
Array destructuring uses position. The first variable gets the first item, the second variable gets the second item, and so on.
Rest syntax collects remaining values into a new array. Default values can fill in missing positions.
Destructuring is useful for function returns, entries loops, swapping variables and handling structured data without repetitive index access.
Pulls values by position.
Collects remaining values into an array.
Used when a position is missing or undefined.
Two variables can be swapped without a temporary variable.
Examples
for (const [key, value] of Object.entries(settings)) {
console.log(`${key}: ${value}`);
}
const row = ["HTML", 94, true]; console.log(row[0], row[1], row[2]); // The meaning of each position is easy to forget.
Code patterns
These are the patterns developers search for constantly: reading items, copying arrays, choosing map/filter/reduce, sorting with a comparator, destructuring and using Set or Map.
Read positions into names.
const colors = ["red", "green"]; const [primary, secondary] = colors;
Use commas to ignore items.
const values = ["a", "b", "c"]; const [first, , third] = values;
Collect the remaining items.
const values = [1, 2, 3, 4]; const [first, ...rest] = values;
Destructure index and value.
for (const [index, value] of values.entries()) {
console.log(index, value);
}
Rules that matter
Arrays and collections are not just storage. They describe whether the code is selecting, transforming, grouping, counting, sorting or enforcing uniqueness.
It is great for pairs and small tuples.
Rest collects the unused tail.
Defaults make missing values explicit.
Too many positions becomes unreadable.
If positions need explanation, an object may be better.
It makes key-value loops much clearer.
Production thinking
Destructuring removes repetitive index access and gives values names at the moment they are read.
Clear variable names make rendered labels and state updates easier to audit.
Production destructuring should stay small and obvious. Deep clever destructuring is hard to maintain.
Destructured content values should have clear names before being rendered into page text.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
A collection method should tell the reader what kind of operation is happening before they read the callback body.