EN
JavaScript - unpack array elements into separate variables
0 points
In this article, we would like to show you how to destructure (unpack) array elements into separate variables in JavaScript.
In this section, we use simple array notation to "unpack" array into separate variables by index.
xxxxxxxxxx
1
var array = ['a', 'b']
2
3
var a = array[0];
4
var b = array[1];
5
6
console.log(a); // a
7
console.log(b); // b
In this section, we use array destructuring to unpack array items into separate variables by index.
xxxxxxxxxx
1
let [a, b] = ['a', 'b'];
2
3
console.log(a); // a
4
console.log(b); // b
xxxxxxxxxx
1
var array = ['a', 'b'];
2
var [a, b] = array;
3
4
console.log(a); // a
5
console.log(b); // b