3

My question may be already answered, but I could not find it not in Search Engines google or bing doesn't like '+' (plus) sign in search request.

Anyway, why this is zero

+[[]][0] // = 0

and this is one

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

UPD: Michael Berkowski have a good answer, but I steal don't understand one thing

if [[]][0] evaluates to an empty array, then why ++[] is ReferenceError: Invalid left-hand side expression in prefix operation

UPD2: now I get it.. it seems I was trying to type ++0 in console and getting an Error, but I should be using var a = 0; ++a

obenjiro
  • 3,665
  • 7
  • 44
  • 82
  • They do when you use `""`. Though I doubt you'd have found a ready-made explanation for this exact code -- you have to break it down into its components and research each language feature individually. Where did you see this, and why do you need it? – Lightness Races in Orbit Nov 25 '12 at 20:28
  • @LightnessRacesinOrbit I saw that in some videocast about javascript.. this was some kind of joke, but i really want to understand that.. – obenjiro Nov 25 '12 at 20:36
  • 1
    @Ai_boy Regarding the addendum to your question, I'm not certain, but that might even vary by browser. In Chrome, I see the same result. You can apply the prefix/postifx `++` to a variable, but not to a scalar value. You can't do `++(+"")` either, although you can evaluate `+""` to `0` – Michael Berkowski Nov 25 '12 at 20:48

1 Answers1

8

This is best explored by breaking down the way its components evaluate.

[[]][0] alone evaluates to the empty array []. By adding + in front, you cast its string representation to an integer 0 (like saying +4 or -3) via a unary positive operator. +0 is just 0.

++ as a numeric operator, also casts the empty string to an integer 0, but applies its operation (the prefix increment) resulting 1.

[[]][0]
// [] empty array
[[]][0].toString()
// ""

// Unary + casts the empty string to an integer
+("")
// 0

// Prefix increment on an empty string results in 1 (increments the 0)
var emptyString = "";
++emptyString;
// 1
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • 2
    More precisely, `++` first casts to an integer and then (pre)increments. That's why you don't need `++(+[[]][0])`. – Ted Hopp Nov 25 '12 at 20:30
  • @TedHopp Yes that is a better explanation. – Michael Berkowski Nov 25 '12 at 20:31
  • could you plz explain "applies the prefix increment to 1." more in detail? – obenjiro Nov 25 '12 at 20:37
  • @MichaelBerkowski i'm talking about second case "++[[]][0] // = 1" – obenjiro Nov 25 '12 at 20:38
  • @Ai_boy `++0` is `1`. `++` takes the number next to it, increments by one, and returns the result. – bfavaretto Nov 25 '12 at 20:43
  • @bfavaretto now I get it.. it seems I was trying to type `++0` in console and getting an Error, but I should be using `var a = 0; ++a` – obenjiro Nov 25 '12 at 20:45
  • The question is... why does `++` work on `[5][0]` but not on `5`? Similarly, on `["5"][0]` but not on `"5"` – aditsu quit because SE is EVIL Jul 03 '15 at 20:35
  • @aditsu What specifically are you trying? Are you doing `++[5][0]` and then trying `++5`? If so, you simply cannot increment an int primitive like `++5`. Doing `[5][0]` returns the zero'th index of the array `[5]` then increments it, so rather than incrementing the primitive `5` you are incrementing the value referenced at the `[0]` position of the array `[5]`. Since you are incrementing the array value by its reference, it seems to work. It's like doing `x = [5]; ++x[0]` which increments it to 6. – Michael Berkowski Jul 03 '15 at 20:44
  • @aditsu The number `5` itself [is immutable](http://stackoverflow.com/questions/8248568/javascript-numbers-immutable) but the array element is mutable. – Michael Berkowski Jul 03 '15 at 20:46