7
alert(
    (![]+[])[[]-[]]+
    (([]+[])+([][[]]))[[]-[]]+
    (([]+[])+([][[]]))[!![]-[]]
);

Heres' the fiddle: http://jsfiddle.net/leeny/6VugZ/

How exactly is this cryptic piece of code working?

PhD
  • 11,202
  • 14
  • 64
  • 112

1 Answers1

7
        vvvvvvv [0]
(![]+[])[[]-[]]                            = "false"[0]
^^^^^^^^ "false"

                  vvvvvvv again [0]
(([]+[])+([][[]]))[[]-[]]                  = "undefined"[0]
^^^^^^^^^^^^^^^^^^ "undefined"

                  vvvvvvvvv this time [1]
(([]+[])+([][[]]))[!![]-[]]                = "undefined"[1]
^^^^^^^^^^^^^^^^^^ again "undefined"

Thus you get "f"+"u"+"n" === "fun".

Further explanation

"false"

![] is false. +[] simply acts as a transformation into a string. Thus we gain the string "false".

"undefined"

One of the operands needs to be a string. This is being done by []+[]. The actual undefined is created in the right hand side: [][[]] === [][0], the first entry of an empty array is undefined.

Community
  • 1
  • 1
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • 1
    As a side note, `(([]+[])+([][[]]))` seems like overkill to me, `([]+[][[]])` appears to suffice. – Neil Mar 08 '13 at 21:20
  • @Neil: There are other overkills too: `[[]-[]] === [+[]]`. I believe `(![]+[])[+[]]+([]+[][[]])[+[]]+([]+[][[]])[+!![]]` is the shortest sequence which creates the same result. – Zeta Mar 08 '13 at 21:24