Search code examples
javascriptdestructuringassignment-operator

What do square brackets mean on the left-hand side of variable assignment (`[ (…) ] = (…)`)?


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 Opera 10.30, and Firefox 3.6.x, but it’s not okay for Opera 10.60, and Chrome.

It’s just curiosity: do you have any reference or link that says this code is compliant to the ECMAScript standard or not?


Solution

  • This is a feature called destructuring assignment, which was added in JavaScript 1.7 and ECMAScript 6. It is not a part of ECMAScript 5: What is cross browser support for JavaScript 1.7's new features? Specifically array comprehensions and the "let" statement