Beautiful right? This messy line of brackets, exclamation points and plus signs evaluates to the string “2014”. You can try it by pasting it into your browsers console or using Node.js.
Let’s have a closer look at the first part of it:
This produces an array with one entry: 2. If we break it down even further, it starts to make sense:
This code consists of four elements (differently colored). On the right we got a simple, empty array. The plus sign in front of it automatically casts it into a number, because that’s how loosely typed languages work. We can prove it by running this:
All we have to do now is simply cast true back into a number by using the plus operator:
Now if we chain two of those together, we get our first value of “2014”; “2”!
And that’s the whole magic. The rest is pretty much the same, I won’t explain it all here. There is a site called JSFuck.com which can produce this kind of code. Quite interesting!