First Principles
Programming

Array Destructuring in Javascript

Pulling values out of arrays by position — skipping, rest, defaults, nesting, swapping, and other iterables.

By Fadjar Rafi. Published 29 Sept 2024. 2 min read.

Contents
  1. The basics
  2. A few useful patterns
  3. Where you'll see it

We looked at destructuring objects already — arrays work the same way, except values are matched by position instead of by name.

The basics#

const fruits = ['apple', 'banana', 'grape', 'orange']

const [first, second, third, fourth] = fruits

console.log(first) // 'apple'
console.log(second) // 'banana'

The first variable takes the first element, the second takes the second, and so on.

Array elements matched to variables by their position

A few useful patterns#

Skipping elements#

Leave a gap with a comma to skip an element you don't need:

const fruits = ['apple', 'banana', 'grape', 'orange']
const [first, , third] = fruits

console.log(first) // 'apple'
console.log(third) // 'grape'

Rest#

Collect whatever's left into an array with ...:

const fruits = ['apple', 'banana', 'grape', 'orange']
const [first, second, ...remaining] = fruits

console.log(first) // 'apple'
console.log(remaining) // ['grape', 'orange']

Default values#

Set a fallback for elements that might be missing:

const colors = ['red']
const [primary, secondary = 'blue'] = colors

console.log(primary) // 'red'
console.log(secondary) // 'blue' — the default

Nested arrays#

You can reach into nested arrays by mirroring their shape:

const fruits = ['apple', ['red', 'green'], 'banana']
const [name, [color1, color2]] = fruits

console.log(name) // 'apple'
console.log(color1) // 'red'
console.log(color2) // 'green'

Mixing with objects#

Array and object destructuring combine when the data is mixed:

const data = ['apple', { color: 'red', weight: 150 }]
const [name, { color, weight }] = data

console.log(name) // 'apple'
console.log(color) // 'red'
console.log(weight) // 150

Function parameters#

Destructure right in the parameter list:

function analyzeFruits([first, second, ...others]) {
  console.log(`First: ${first}`)
  console.log(`Second: ${second}`)
  console.log(`Others: ${others.join(', ')}`)
}

analyzeFruits(['apple', 'banana', 'grape', 'orange'])

Swapping values#

A neat trick — swap two variables without a temporary:

let a = 'apple'
let b = 'banana'

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

console.log(a) // 'banana'
console.log(b) // 'apple'

Other iterables#

It isn't limited to arrays — strings and sets work too, since they're iterable:

const [x, y, z] = 'xyz'
console.log(x, y, z) // 'x' 'y' 'z'

const [head, ...rest] = new Set([1, 2, 3])
console.log(head, rest) // 1 [2, 3]

Where you'll see it#

Two everyday examples: splitting a string, and React's useState, which returns a [value, setter] pair you name yourself.

const [year, month, day] = '2024-09-29'.split('-')

const [count, setCount] = useState(0)

Once positional destructuring clicks, you'll spot it all over — function returns, hooks, and anywhere a function hands back more than one value.