Tsallaka zuwa abun ciki
Justice Tech School

Array Destructuring and Spread

Destructuring unpacks arrays into variables. Use defaults, skip items with commas and collect the rest with ...rest. Swap variables without a temp.

Example
const [first, , third = 0, ...rest] = [1, 2, undefined, 4, 5];
console.log(first, third, rest);
let a = 1, b = 2;
[a, b] = [b, a];
console.log(a, b);

Exercise

Collect the rest.

const [x, ...] = arr;

Quiz Test

How do you swap a and b?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.