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.
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:
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.
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 outputjavascript
Read the code. What exactly will it print? Commit to an answer before you look.
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.
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
Your output
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.
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`.