Array Destructuring

JavaScript ES6+ · ✓ verified by execution on 2026-08-13

In earlier lessons, you learned how to access elements in an array using their numerical index (e.g., colors[0]). While this works perfectly fine, it can quickly become tedious and repetitive when you need to extract multiple values into their own individual variables. Modern JavaScript provides a sleek, elegant syntax to solve this exact problem: array destructuring.

Array destructuring allows you to “unpack” values from an array and assign them to distinct variables in a single, readable statement. Let’s see how it radically cleans up our code.

The Destructuring Syntax

Destructuring uses square brackets [] on the left side of the assignment operator (=). It essentially mirrors the array literal syntax used to create arrays. JavaScript takes the variable names you provide inside the brackets and assigns them values from the array based entirely on their position.

javascript
const colors = ['red', 'green', 'blue'];
const [primary, secondary] = colors;
console.log(primary);
console.log(secondary);
Output
red
green

The first variable gets the first item, the second variable gets the second item, and so on. Let’s use our visual stepper to watch exactly how JavaScript maps these array positions to the new variable names during the unpacking process:

javascript · visualize
const colors = ['red', 'green', 'blue'];
const [primary, secondary] = colors;
console.log(primary);
console.log(secondary);

Skipping Unwanted Items

You rarely need every single item in a large array. What if you only want the first and third items? Destructuring allows you to skip elements simply by leaving an empty “slot” between the commas. The empty space tells JavaScript to ignore that specific position and move on to the next one.

javascript
const scores = [10, 20, 30, 40];
const [first, , third] = scores;
console.log(first);
console.log(third);
Output
10
30

The Rest Operator and Defaults

If you want to extract a few items but bundle all the remaining ones into a brand new array, you can use the rest operator (...). This must always be the very last element in your destructuring pattern.

Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

const nums = [1, 2, 3, 4, 5];
const [first, ...rest] = nums;
console.log(first);
console.log(rest);
Output
1
[ 2, 3, 4, 5 ]

Additionally, if you try to destructure an array that is shorter than your pattern, the variables that don’t find a matching element will be set to undefined. To prevent bugs, you can provide default values right inside the brackets.

javascript
const arr = [10];
const [a = 1, b = 2] = arr;
console.log(a, b);
Output
10 2

Swapping Variables

One of the most practical and popular use cases for array destructuring is swapping the values of two variables. Historically, swapping two values required creating a temporary third variable to hold the data so it wouldn’t be overwritten. With destructuring, we can create an array with the two values and immediately destructure them back out in reverse order!

javascript
let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a, b);
Output
2 1

Destructuring Does Not Mutate

A very common beginner misconception is that destructuring “removes” or deletes the unpacked items from the original array. This is entirely false. Destructuring only reads the values to assign them to your new variables. The original array remains completely intact.

['red', 'green', 'blue'] primary secondary
Destructuring by position: the first two elements of ['red', 'green', 'blue'] are bound to primary and secondary, and the third is simply not taken.

Mastering array destructuring will make your JavaScript much more concise, expressive, and modern!

Check yourself

Does array destructuring mutate the original array?

Reveal answer

Yes, it modifies the original array only if you use the rest operator. — Destructuring simply reads from the array to assign variables; it never mutates the original array.

How can you skip an item while destructuring an array?

Reveal answer

You cannot skip items; you must unpack them all. — You can ignore return values that you are not interested in by leaving an empty slot between commas.

What happens if you try to destructure an index that does not exist in the array?

Reveal answer

The program crashes. — If the array is shorter than expected, the remaining variables are safely assigned undefined.

Challenges

Challenge 1 +20 XP

Extract the first and third items from the array into variables `gold` and `bronze`.

javascript
  • Test 1 — expects "USA Japan\n"
Need a hint? (−25% XP)

Use an empty comma slot to skip the second item.

Show solution (0 XP)
const medals = ['USA', 'China', 'Japan'];
const [gold, , bronze] = medals;
console.log(gold, bronze);

Challenge 2 +20 XP

Swap the values of `x` and `y` using array destructuring.

javascript
  • Test 1 — expects "oranges apples\n"
Need a hint? (−25% XP)

Use [x, y] = [y, x] to swap the values.

Show solution (0 XP)
let x = 'apples';
let y = 'oranges';
[x, y] = [y, x];
console.log(x, y);

🐞 Bug Hunt +30 XP

Find and fix the bug in this array destructuring code. It's supposed to skip the first two items and print the third.

This code runs. It just does the wrong thing. Read it, find the defect, fix it — the tests below decide when you are right.

javascript
  • Test 1 — expects "Earth\n"
Need a hint? (−25% XP)

Remember that destructuring matches by position, not by variable name. How do you skip the first two positions?

Show solution (0 XP)
const planets = ['Mercury', 'Venus', 'Earth', 'Mars'];
const [, , Earth] = planets;
console.log(Earth);