3

I realize that this is intended as a joke, but someone posted this and it alerts "fail" in the browser, what is going on that makes this happen?

alert((![]+[])[+[]]+(![]+[])[+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]);
asimes
  • 5,749
  • 5
  • 39
  • 76
  • Someone posted it on Facebook, I don't know where it comes from otherwise – asimes Jun 19 '14 at 00:07
  • Anyway, the main "trick" to this is that `(![]+[])` results in a string - `"false"` - which is then indexed as `"false"[+[]]` -> `"false"[0]"` -> `"f"`, etc – user2864740 Jun 19 '14 at 00:08
  • 2
    And `([]+[][[]])` results in the string `undefined`. The two strings provide all you need to get `f`, `a`, `i`, and `l` with appropriate indexing. – Barmar Jun 19 '14 at 00:09
  • 1
    I found it interesting enough on a serious level I must say, gives you good insight into programming possibilities – myfunkyside Jun 19 '14 at 00:12
  • I don't think this is not a serious question, but I did not realize it was answered before (thank you user2864740), closing due to duplicate... – asimes Jun 19 '14 at 00:16
  • *"what is going on that makes this happen"* type conversion ;) – Felix Kling Jun 19 '14 at 00:21

1 Answers1

1

Here is a good blog post about this topic: http://patriciopalladino.com/blog/2012/08/09/non-alphanumeric-javascript.html

You basically create strings ("false","undefined" and others) and again use addition of true's to get the indexes.

For example the first letter (f): (![]+[])[+[]] You use ![] to get false, and add [] to convert it into a string. The content within the parenthesis is now "false". You then access it like an array, and ´+[]` converts to 0 (false as a number).

orhanhenrik
  • 1,407
  • 8
  • 11