Destructuring arrays

suggest change
const myArr = ['one', 'two', 'three']
const [ a, b, c ] = myArr

// a = 'one', b = 'two', c = 'three'

A missing element can take a default. See Default value while destructuring.

With destructuring array, we can swap the values of 2 variables easily:

let a = 1;
let b = 3;

[a, b] = [b, a];
// a = 3, b = 1

We can specify empty slots to skip unneeded values:

[a, , b] = [1, 2, 3] // a = 1, b = 3

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents