My colleague, pasted an interesting javaScript idiom to our developer chat, some time ago, and it immediately caught my attention. If you cast an array literal to a number, prepending it with a ‘+’ operator, it will evaluate to 0.

var a = +[];
// a is 0

Heh, something so logical, yet obscure, immediately inspired me to take this a bit further.

Let’s try and use the increment operator to turn that 0 into a 1

var a = ++[];
// Throws an error

Hmmm, an error? Well of course, since you cannot use an increment operator on an array.

Let us try and bypass that.

var a = ++[[]][0];
// a is 1

So, we have an array literal that contains another array literal, and we apply the increment operator to the first element of our array, which is again, an array.

And well, well, look at here, we didn’t get an error! And our array was first cast to a number and the increment operator was able to increment it from 0 to a 1, how about that!

Sweet, we now have, an obscure 1 :)

Let’s replace that 0 in the code above, with the obscure 0

var a = ++[[]][+[]];
// a is 1

For homework, figure this one out

var a = ++[[]][+[]]<<++[[]][+[]];
// a is 2

Okay, it’s time to bring it to the next level.

With 0 and 1, we can represent any number that we want, but having a 2 will make it even easier.

Let’s try and represent 72, with our obscure idioms.

var a = 72;
// which is equivalent to:
a = Math.pow(2,6) + Math.pow(2,3); // = 2^6 + 2^3 = 64 + 8 = 72
// which is equivalent to:
a = (1<<6) + (1<<3);
// which is equivalent to:
a = (1<<((2<<2)+2)) + (1<<(2+1));
// let us now replace each
// 1 with (++[[]][+[]]) and
// 2 with (++[[]][+[]]<<++[[]][+[]])
a = ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]]))+(++[[]][+[]]<<++[[]][+[]]))) + ((++[[]][+[]])<<((++[[]][+[]]<<++[[]][+[]])+(++[[]][+[]])));
// a is *still* 72 :)

Seemples! Now what would happen if you try to do something like this:

var a = 72;
var ch = String.fromCharCode(a);
// ch is "H"

I guess you’re seeing where I’m getting with this, by now ;)

Here’s my obscure “Hello world” code to showcase this exercise.

var a='',l,c=String.fromCharCode, arr=[
((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]]))-(++[[]][+[]]<<++[[]][+[]]))) + ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]))-(++[[]][+[]]))), ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]]))-(++[[]][+[]]<<++[[]][+[]]))) + ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]))+(++[[]][+[]]))) + ((++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]])) + (++[[]][+[]]), ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]]))-(++[[]][+[]]))) - ((++[[]][+[]])<<((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]))) - ((++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]])), ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]]))-(++[[]][+[]]))) - ((++[[]][+[]])<<((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]))) - ((++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]])), ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]]))-(++[[]][+[]]))) - ((++[[]][+[]])<<((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]))) - (++[[]][+[]]), ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]))+(++[[]][+[]]))),  ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]]))-(++[[]][+[]]))) - ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]))-(++[[]][+[]]))) - (++[[]][+[]]), ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]]))-(++[[]][+[]]))) - ((++[[]][+[]])<<((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]))) - (++[[]][+[]]) , ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]]))-(++[[]][+[]]))) - ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]))-(++[[]][+[]]))) - ((++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]])) - ((++[[]][+[]])<<(++[[]][+[]])), ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]]))-(++[[]][+[]]))) - ((++[[]][+[]])<<((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]))) - ((++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]])), ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]]))-(++[[]][+[]]<<++[[]][+[]]))) + ((++[[]][+[]])<<(((++[[]][+[]]<<++[[]][+[]])<<(++[[]][+[]]))+(++[[]][+[]]))) + ((++[[]][+[]])<<(++[[]][+[]]<<++[[]][+[]]))
];
while(l=arr.shift())a+=c(l);

alert(a);
// Will alert "Hello world"

Neat, right? ;)