24

Why does the first line work while the second line throws run-time exception?

The first line:

[[]][0]++; //this line works fine

The second line:

[]++; //this lines throws exception
7
  • 1
    Yeah, I think a lot of people read Dave's blog: davidwalsh.name/thinking-javascript
    – Mark
    Aug 26, 2017 at 9:05
  • 7
    Because Javascript. Aug 26, 2017 at 13:49
  • Why would you expect otherwise? Can you explain why the first line should throw an exception, or what the second line should do when it would not?
    – Bergi
    Aug 26, 2017 at 15:44
  • @Bergi They both appear to do the same thing: Increment an empty array. They just do it differently. I would expect them both to throw, as incrementing an array is nonsense
    – Suppen
    Aug 26, 2017 at 16:01
  • 1
    That's how it actually works, as described by the accepted answer. They just appear to do the exact same thing in different ways
    – Suppen
    Aug 26, 2017 at 16:09

3 Answers 3

18
[[]][0]++

is equivalent to

var tmp = [[]];
tmp[0] = tmp[0]+1;

tmp[0] is an empty array, which is cast to the number 0, which increments to 1.

This only works because <array>[<index>]++ looks valid. It takes some type juggling, but it gets there.

But []++ is outright invalid. There's no way to make it make sense.

[] = []+1;

The left-hand side here is indeed invalid. You can't assign to an empty array.

3
  • 1
    This also works with [''][0]++ and [null][0]++ and [false][0]++ because they all cast to 0.
    – Soviut
    Aug 26, 2017 at 9:05
  • 7
    This answer is incorrect. It has nothing to do with "looking" valid. It's just that the ++ operator requires a reference, which [] (a literal) is not.
    – Nikolai
    Aug 26, 2017 at 13:54
  • "You can't assign to an emty array"... indeed now as 2021 you can; [] = []; is a valid empty destructuring assignment of zero expressions to zero variables :-)
    – 6502
    Sep 28, 2021 at 17:15
11

The ++ operator (or indeed any postfix operator) requires the operand to be a "reference" - that is, a value that can be assigned to. [] is a literal, so you can't assign to it. [[]][0] is a valid reference to an element of a temporary array.

0++; // not valid, `0` is a literal.
var a = [];
a++; // ok, `a` is assignable
5

This is a rare case in which Javascript does something that actually makes sense. Consider

x[3]++; // Valid
3++;    // Not valid

If this make sense for you, then what is surprising about

[[]][0]++; // valid
[]++;      // not valid

<array>[index] is "a place" that you can assign or increment. That's all. The fact that you can increment a[<expr>] doesn't imply that you can increment <expr>.

The absurd part is that you can use [] as an index, that has the meaning of converting the array to an empty string "" and then to the number 0, but this is the well known problem of absurd implicit conversions of Javascript. Those implicit conversion rules are a big wart of Javascript and for example imply that 1 == [1] or that both []==false and (![])==false.

Javascript is pure nonsense in a lot of places... but not really here.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.