For a project a developer sent us a .js file with code similar to this:
var myList = [1,2,3];
var a,b,c;
[a,b,c] = myList;
It works in Ope
Here’s an update on the subject: as of JavaScript version 1.7, destructuring assignments are supported by all major browsers: see browser compatibility.
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
– MDN’s documentation
So you can do:
let a, b;
[a, b] = ["Hello", "World"];
console.log(a); // "Hello"
console.log(b); // "World"
Or simply in one line if you're defining the variables:
let [a, b] = ["Hello", "World"];
console.log(a); // "Hello"
console.log(b); // "World"