Unpack Values
Destructuring lets you unpack array values into variables.
It improves readability compared to manual indexing.
JavaScript Tutorial
Array destructuring lets you unpack values into variables in a clean, readable way.
It is especially useful for function returns and swapping values.
Destructuring reduces repeated indexing and makes intent clear.
It also works well with rest syntax and default values.
const [a, b] = array
const [first, , third] = array
const [head, ...tail] = arrayconst coords = [10, 20];
const [x, y] = coords;
console.log(x, y);Assign positions to variables.
const items = ["a", "b", "c"];
const [first, , third] = items;
console.log(first, third);Use commas to skip.
Destructuring lets you unpack array values into variables.
It improves readability compared to manual indexing.
You can skip elements with commas and set default values.
This is useful for optional positions.
Use rest syntax to collect remaining items into a new array.
Rest must be the last element in the pattern.
Without
const first = items[0];
const second = items[1];With
const [first, second] = items;Use defaults for optional positions.
Rest must be the final pattern element.
Keep code readable; use destructuring when it helps.
It assigns values by position from the array.
Yes, use = defaultValue in the pattern.
At the end of the destructuring pattern.
Practice: Destructure a list of three colors into variables.
const colors = ["red", "green", "blue"];
// TODO: destructure into a, b, c
One Possible Solution
const colors = ["red", "green", "blue"];
const [a, b, c] = colors;
console.log(a, b, c);It assigns array items to variables by position.
Yes, by leaving a blank spot between commas.
Use ...rest as the last pattern element.
Try skipping values or adding defaults.