0

Why is

++[[]][0] == 1

But does

++[]

throw an error

Aren't they the same? I would believe the first example executes an index-read on the array so you get the array in the array. And then the increment is executed. If so than why can't I do the second example?

1
  • BTW this isn't a JavaScript-specific "quirk". Any language that has assignment operators will behave similarly, although some might statically prevent you from writing useless statements like the first one and some might refuse to take an array as an operand to addition. Commented Sep 30, 2015 at 8:48

1 Answer 1

5

++ is an assignment operator. It requires a valid left-hand-side operand (even though it can be on the right of ++).

[] is only a value, not something you can assign to.

[[]][0] evaluates to [] but it is a valid left-hand-side, because it points to an element in an existing array. So that works.

To give a hopefully less confusing example:

var a = 1
1++ // throws an error
a++ // works fine

It doesn't matter what value is in a. Worst case, ++ will return NaN, never an error, as long as it can assign the result.

The only JavaScript quirkiness in your example is that +[] + 1 evaluates to 1 because the empty array is coerced to an empty String, then explicitly to zero (+"" is 0), which is then added to 1.

The ++ operator always coerces to number, unlike + which would be satisfied with "" (so [] + 1 turns into "" + "1"). Thus, when decomposing a ++, don't forget to force the operands to number (not that it ultimately matters in your example).

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

5 Comments

So if I'm not mistaken, I could see it like this (can't put new lines in the comment...): ++[[]][+[]] [[]][+[]] = [[]][+[]] + 1; [[]][0] = [[]][0] + 1; [[]][0] = [] + 1; [[]][0] = "" + "1"; // +-operator prefers string concatination, empty array = "" ["1"] // the first value of the array is replaced by "" + "1"
That's even more contrived than your original example (adding +[]) :p You got the evaluation order mostly right, except that the top priority is actually +[] which evals to zero before anything else.
Yes thanks :p the original "puzzle" was: ++[[]][+[]]+[+[]] wich is "10". But I never fully understood the first part until now, thanks!
Actually ++ coerces to number (unlike += 1 which is truly equivalent to = X + 1) so it doesn't happen exactly the same as in my post. In your "puzzle" here, the first part evals to the number 1 (by coercing the empty array to "" and then to 0, and adding 1) rather than the string "1". It is then coerced to a string because the second part ([0]) is coerced to "0".
@YentheO I edited my answer to be clearer on the semantics of ++.

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.