How to use destructuring assignment to set values from array into variables in JavaScript

2 Answers

0 votes
let a, b

[a, b] = [12, 4];

console.log(a);

console.log(b);




/*
run:

12
4

*/

 



answered Nov 13, 2020 by avibootz
0 votes
let a, b
const arr = [7, 9];

[a, b] = arr;

console.log(a);

console.log(b);




/*
run:

7
9

*/

 



answered Nov 13, 2020 by avibootz
...