Sub topic 3
Destructuring and Spreading Arrays
This chapter assumes you know let and const, arrow functions, and how to index an array with arr[0] and read arr.length. It covers two pieces of syntax that use the same brackets and dots in opposite directions. Destructuring unpacks an array into variables. Spread unpacks an array into a new array or into a function call.
Destructuring: Pulling Values Out by Position
Array destructuring assigns elements to variables by their position. The pattern on the left mirrors the shape of the array on the right.
const point = [10, 20, 30];
const [x, y, z] = point;
console.log(x, y, z); // 10 20 30
// Skip positions with empty commas
const [first, , third] = point;
console.log(first, third); // 10 30
// Defaults apply only when the value is undefined
const [a, b, c = 0, d = 0] = [1, 2, null];
console.log(c, d); // null 0The last line shows a common error. A default does not replace null, 0, or ''. It replaces only undefined, which includes positions past the end of the array.
Collecting the Rest
A rest element (...name) gathers every remaining element into a new array. It must come last in the pattern.
const [head, ...tail] = ['a', 'b', 'c', 'd'];
console.log(head); // 'a'
console.log(tail); // ['b', 'c', 'd']
const [only, ...none] = ['x'];
console.log(none); // [] (always an array, never undefined)Swapping Without a Temporary Variable
Destructuring can assign to existing variables, which gives you a one-line swap.
let left = 'L';
let right = 'R';
[left, right] = [right, left];
console.log(left, right); // 'R' 'L'Spread: Expanding an Array in Place
Spread syntax (...arr) expands an array's elements wherever a list of values is expected: inside an array literal or in a function call's arguments.
const base = [2, 3];
const extended = [1, ...base, 4];
console.log(extended); // [1, 2, 3, 4]
const copy = [...base];
copy.push(99);
console.log(base); // [2, 3] (original untouched)
console.log(Math.max(...[5, 9, 2])); // 9Spread works on any iterable, so [...'hi'] gives ['h', 'i'] and [...new Set([1, 1, 2])] gives [1, 2].
Spread Copies Are Shallow
Spread copies the top level only. Nested objects and arrays are shared between the copy and the original.
const grid = [[1, 2], [3, 4]];
const gridCopy = [...grid];
gridCopy[0].push(99);
console.log(grid[0]); // [1, 2, 99] (inner array is shared)For a deep copy of plain data, use structuredClone(grid).
| Syntax | Position | Effect |
|---|---|---|
| `const [a, b] = arr` | Left of `=` | Unpacks by position |
| `const [a, ...rest] = arr` | Left of `=` | Collects remaining elements |
| `[...arr, x]` | Right of `=` | Builds a new array |
| `fn(...arr)` | Call arguments | Passes elements as separate arguments |
Exercise
What does this code log? const [p = 'none', ...q] = [undefined, 0, null]; console.log(p, q);
Show answer
It logs none [0, null]. The first element is undefined, so the default applies. The rest element collects the remaining values, and 0 and null are kept as they are.
Exercise
Given const orig = [{ n: 1 }]; const dup = [...orig]; dup[0].n = 2;, what is orig[0].n, and why?
Show answer
It is 2. Spread makes a shallow copy, so dup[0] and orig[0] refer to the same object.