1/23/2011

# Javascript: \$=-[\$=[]],\$\$=\$/\$+(!\$+{})

After having read this post yesterday. I feel like that was my turn to put my 2 cents. After some tests, I tweeted the javascript code below which return "boNjour":
`\$=-[\$=[]],\$\$=\$/\$+(!\$+{}),_=-~-~-~\$,__=\$\$[(_*_)+~\$],\$\$[_*_]+__+\$\$[\$]+\$\$[-~(_*_)]+__+\$\$[_+-~-~\$]+\$\$[_+-~\$]`

# How ? Where are the letters ?

The first statement is:
```\$ = -[\$=[]]; \$ == 0```
With this \$ I can get all the letters I want for the word "bonjour" thanks to the second statement:
```\$\$ =  \$/\$+(!\$+{}); \$\$ == "NaNtrue[object Object]"```

# \$/\$+(!\$+{}) ?

First of all, I wanted an n:
```\$/\$ == 0/0 == NaN typeof(\$/\$) == "number"```
Next an u r:
```\$          == 0; !\$         == true typeof(!\$) == "boolean"```
And finally the b o j:
```true+{} == "true[object Object]" // Thanks to type coercion !\$+{}   == "true[object Object]"```
Then, I needed to convert NaN (a number) to a string. Which is easy with javascript's type coercion:
`number + string = string`
So:
```\$/\$ + (!\$+{}) == "NaNtrue[object Object]" \$\$ = \$/\$ + (!\$+{});```

The last statement of the code do the following:
`\$\$+\$\$+\$\$+\$\$+\$\$+\$\$+\$\$ == "boNjour"`

# But... where are the numbers ?

The NOT operator is the anwser !
The ~ operator in JavaScript means -(N+1) so -~ = +1
if \$ = 0 then -~-~-~\$ = 3

Remember, we already have 0 (with \$):
`\$ == 0`
I started with a 3, because 3*3 = 9, (3*3)-1 = 8, (3*3)+1 = 10, (3+(1+1)) = 5 and 3+1 = 4. We can get all these numbers by playing around with \$ & ~ :
```-~-~-~0 == -~-~-~\$ == 3 _ = -~-~-~\$```
Let's do it !
```_*_ == 9 \$\$[_*_] == "b";```
o is used 2 times so it has his own variable __ in order to reduce the code's length:
```(_*_)+~\$ == 9 + -1 == 8 __ = \$\$[(_*_)+~\$]; __ == "o";```
```\$\$ == "N"; \$\$[\$] == "N";```
```1 + (_*_) = 1 + 9 = 10 //With N=9, -(N+1) = -10 so ~9 == -10 -~(_*_) == 10 \$\$[-~(_*_)] == "j";```
```3 + (1+1) = 3 + -~-~0 = 5 _ + -~-~\$ == 5 \$\$[_+-~-~\$] == "u";```
```3 + 1 == 4 _ + -~\$ == 4 // N=0, -(-(N+1)) = -(-1) = 1 \$\$[_+-~\$] == "r";```
And here we are:
`\$\$[_*_]+__+\$\$[\$]+\$\$[-~(_*_)]+__+\$\$[_+-~-~\$]+\$\$[_+-~\$] == "boNjour";`