Destructuring arrays
suggest changeconst 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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents