Languages

JavaScript - assign array values to multiple variables?

0 points
Asked by:
May87
827

Is the code below a valid code? Seems like for some browsers it works fine and for others it doesn't work at all.

My code:

// ONLINE-RUNNER:browser;

var array = [1, 2, 3];
var a, b, c;

[a, b, c] = array;

console.log(a); // 1
console.log(b); // 2
console.log(c); // 3
1 answer
0 points
Answered by:
May87
827

This feature is called a destructuring assignment and has been added in ES6. This is the reason it may not work in some browsers.

See also

  1. JavaScript - ECMAScript / ES versions and features

References

  1. Destructuring assignment - JavaScript | MDN
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join