JavaScript Array Destructuring

Unpack an Array Into Variables

JavaScript Array Destructuring

Pull values out of an array and into named variables in one line — with defaults, skipping, and rest.

4 min read Level 2/5 #arrays#destructuring#syntax
What you'll learn
  • Destructure arrays into named variables
  • Provide default values
  • Use rest to capture the remainder
  • Swap two variables in one line

Destructuring lets you pull values out of an array (or object) and into named variables — all in one expression.

The Basics

Pull out positional items script.js
const colors = ["red", "green", "blue"];

const [a, b, c] = colors;
console.log(a, b, c); // "red" "green" "blue"
▶ Preview: console

Compare with the verbose version:

// Verbose:
const a = colors[0];
const b = colors[1];
const c = colors[2];

// Destructured:
const [a, b, c] = colors;

Same effect, one line.

Skipping Items

Leave a slot blank with a comma:

Skip items with commas script.js
const [a, , c] = ["red", "green", "blue"];
console.log(a); // "red"
console.log(c); // "blue"
▶ Preview: console

Defaults

If the array doesn’t have a value at that position, you can supply a default.

Defaults script.js
const [a = "x", b = "y", c = "z"] = ["red", "green"];
console.log(a); // "red"
console.log(b); // "green"
console.log(c); // "z"   ← default
▶ Preview: console

The default only kicks in when the slot is undefined. null doesn’t trigger it.

Rest — Capture the Rest

... at the end captures whatever’s left into an array.

Rest pattern script.js
const [first, second, ...others] = [1, 2, 3, 4, 5];
console.log(first);  // 1
console.log(second); // 2
console.log(others); // [3, 4, 5]
▶ Preview: console

Swapping Two Variables

The cleanest swap you’ll ever write.

Swap two variables script.js
let a = 1;
let b = 2;

[a, b] = [b, a];

console.log(a, b); // 2 1
▶ Preview: console

Destructuring Function Return Values

A function that returns an array becomes much nicer to call.

Destructure a function result script.js
function minMax(numbers) {
  return [Math.min(...numbers), Math.max(...numbers)];
}

const [lo, hi] = minMax([5, 1, 9, 3, 7]);
console.log(lo); // 1
console.log(hi); // 9
▶ Preview: console

This pattern is everywhere in React (const [count, setCount] = useState(0)) and other modern libraries.

Destructuring Function Parameters

You can destructure right in the parameter list:

Destructure parameters script.js
function greet([first, last]) {
  console.log(`Hello, ${first} ${last}!`);
}

greet(["Ada", "Lovelace"]);  // "Hello, Ada Lovelace!"
▶ Preview: console

Try It Yourself

Exercise

Swap two values

Difficulty 2/5~2 min
Given `let x = 1; let y = 2;`, swap the two values using array destructuring **in one statement**. After the swap, `x` should be `2` and `y` should be `1`. Log both.
solution.js
let x = 1;
let y = 2;
// swap them in one line
3tests will run
💡 Show hint
`[x, y] = [y, x];` — that single line does the swap.
✅ Show solution
let x = 1;
let y = 2;
[x, y] = [y, x];
console.log(x, y);

Up Next

Last lesson of this chapter: sorting.

JavaScript Array Sort →