tags:

views:

138

answers:

2

Yeah, it works in my firebug console. Why does something like this present no syntax error?

[] = 5; [] = doThis(); [] = (function() {})();

Just curious about why it's allowed.

+2  A: 

I get a "Uncaught ReferenceError: Invalid left-hand side in assignment" in Chrome. IE8 throws "Error: Cannot assign to '[object]'". FF does, indeed, not throw an error.

This is most likely just a failing of the Firefox JavaScript engine, and in any case, does not seem to affect the functionality of the array literal [].

Ryan Kinal
Aha, fail on Firefox and on me, as I did not actually test this in another browser.
Iggyhopper
+11  A: 

Looks like Javascript in FF allows assignment using matching so you can set multiple variables in one shot:

[x,y] = [5,6]; // sets x to 5 and y to 6

"Obviously", some JS implementations are very forgiving, and will silently ignore any extra values, so this does the same:

[x,y] = [5,6,7];

and this would just not assign anything:

[] = [5,6,7];

and on top of that, it looks like FF is even willing to go with a case that doesn't match an array at all:

[x] = 5; // makes x undefined

so if you use [] = whatever then "nothing gets assigned an undefined value", so it boils down to just evaluating the RHS.

(Disclaimer: this is all just guessing the meaning by running stuff in FF...)

Eli Barzilay
Wow, that's even weirder.
Iggyhopper
Very nice, Eli! +1
Ryan Kinal
There's a detailed thread about mozilla's destructuring assignment syntax here: http://stackoverflow.com/questions/204444/destructuring-assignment-in-javascript
no