13

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.

1
  • It is an error in Chrome btw...I think it's just ignoring your setting and moving on along. Commented Jul 7, 2010 at 1:43

2 Answers 2

14

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...)

Sign up to request clarification or add additional context in comments.

1 Comment

There's a detailed thread about mozilla's destructuring assignment syntax here: stackoverflow.com/questions/204444/…
2

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 [].

1 Comment

Aha, fail on Firefox and on me, as I did not actually test this in another browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.