0

The following code alerts "n". How does this code work?

alert(([]+[][1])[[[+!-[]][0]]])
nicael
  • 18,550
  • 13
  • 57
  • 90
  • 1
    This may be related: http://stackoverflow.com/q/15978204/1249581. – VisioN Jun 25 '14 at 13:15
  • Have a look here : http://security.stackexchange.com/questions/8263/javascript-written-only-with-brackets – Cedric Jun 25 '14 at 13:15
  • 7
    Wait, "unclear what you're asking"? Whats unclear? – nicael Jun 25 '14 at 13:15
  • 2
    Agreed; there may be good reasons to close this (probably a duplicate) but it's way above the bar for clarity around here. – Pointy Jun 25 '14 at 13:16
  • Im really not going to interprete this expression but, might be you are trying to print the first letter of a string representing a non existen numeric expression resultin in NaN and the first letter is N – Banana Jun 25 '14 at 13:17
  • http://patriciopalladino.com/blog/2012/08/09/non-alphanumeric-javascript.html – epascarello Jun 25 '14 at 13:18
  • 1
    @nicael We require that questions have a clear problem statement to be useful to future visitors; your initial title and body was not clear nor useful to future visitors. – George Stocker Jun 25 '14 at 13:19
  • 2
    @nicael work through the subexpressions in your browser console. You'll understand it better if you do that anyway. Also note that there are no useful JavaScript coding skills to be learned from this, but it may help recognize weird buggy behavior caused by code that *accidentally* stumbles into such trickery. – Pointy Jun 25 '14 at 13:22
  • There is no way to reopen it, @Pointy? – nicael Jun 25 '14 at 13:24
  • 1
    @nicael I voted to reopen, as did three others. Really, just start with seeing what `([]+[][1])` looks like in your browser console. – Pointy Jun 25 '14 at 13:26
  • @VisioN: yea, that's a better close vote :-) – Cerbrus Jun 25 '14 at 13:35

1 Answers1

5

So, it alerts ([]+[][1])[[[+!-[]][0]]].

Let's break that up:

    -[]      // -0   (Apparently -`array` == -0)
   !-[]      // true (Boolean NOT on -1 == true)
  +!-[]      //  1   (+ casts `true` to a number (1))
 [+!-[]]     // [1]  (add array brackets) (wow that's cheap)
 [+!-[]][0]  //  1   (get 1st element of the array) 
[[+!-[]][0]] // [1]  (add array brackets)

    []       // []          (Empty array)
    [][1]    // "undefined" (empty string @ 1 == undefined)
 []+[][1]    // ""          (undefined + array == undefined)
([]+[][1])   // "undefined"

"undefined"[[1]]         // "n" (Get the character at zero-indexed position `1`)
([]+[][1])[[[+!-[]][0]]] // "n"

The extra square brackets don't matter much, when accessing an array:

[1][[[[[[0]]]]]] === [1][0] === [1]["0"] === 1;

This is because, behind the scenes, the supplied index is converted to a string:

[[[[[0]]]]].toString() === "0";
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • 1
    THX, Cerbus. Now its clear. – nicael Jun 25 '14 at 13:33
  • 1
    Actually the first part of expression is not interperted in this way. Here []+[][1] first is evaluated the second empty brackets. [] - you get an empty array. [][1] - indexing on the empty array returns you undefined. [] + [][1] - casts the empty array to empty string and empty string plus undefined gives you "undefined" as as string. The other part is correct. You can try to add parentheses on different places to check the precedence. – Viktor Bahtev Jun 25 '14 at 13:45
  • Oh, yea, you're right! I did check my code in the console, though, `[]+[]` does in fact equal `""`, and `""[1] == undefined`, but that's not what's happening, indeed. Fixed my answer. – Cerbrus Jun 25 '14 at 13:49