If you try to run following code you will see that
(![]+[])[+1]
returns "a";
(![]+[])[+2]
returns "l";
(![]+[])[+3]
returns "s".
And so on.
Why?
Asked
Active
Viewed 179 times
1

João Paulo Macedo
- 15,297
- 4
- 31
- 41

windvortex
- 55
- 3
-
8Have you *at least* tried looking at what the individual parts of those expressions evaluate to? – Pointy Aug 21 '14 at 13:54
-
2(![]+[]) is "false" => (![]+[])[+2] is "l", not "1" (one). – ROMANIA_engineer Aug 21 '14 at 13:54
-
1`(![]+[])` evaluates to `"false"` and then you are simply accessing the characters of the string as an array. The reason why is left as an exercise to the reader. :) – dee-see Aug 21 '14 at 13:54
3 Answers
12
(![]+[])
returns "false"
in javascript. The +1
takes the letter at index 1
. Therefore a
.

Danny
- 7,368
- 8
- 46
- 70
-
3`(![]+[])` returns the string `"false"` and not the boolean value, just to make things clear. – dee-see Aug 21 '14 at 13:56
10
Let's break it down piece by piece.
> ![]
false
Array literals are truthy even when empty, so the negation is boolean false.
> ![]+[]
"false"
Adding a boolean (the false from above) to an empty array results in the string version of "false"
, thanks to JS's strange rules for adding arbitrary objects.
> (![]+[])[1]
"a"
> (![]+[])[3]
"s"
(etc)
is equivalent to:
> "false"[1]
"a"
> "false"[3]
"s"
and so on -- indexing a string returns the character at that index.
Finally,
> +[]
0
so
> "false"[+[]]
"f"
and putting it all together:
> (![]+[])[+[]]
"f"

dee-see
- 23,668
- 5
- 58
- 91

kevingessner
- 18,559
- 5
- 43
- 63
1
You're indexing the result of the first expression as a string.
(![]+[]) => false
false[1] => "false"[1] => a

ajm
- 19,795
- 3
- 32
- 37
-
`(![]+[])` returns the string false. `false[1]` returns `undefined`, there is no conversion to string that would happen. – dee-see Aug 21 '14 at 13:57