0
console.log(++[[]][+[]]+[+[]]);

Why in the world does this print out 10 and not something else...? Its been driving me crazy for too long. I found this while reading the following article:

http://tutorialzine.com/2013/12/the-10-weirdest-programming-languages/

Perhaps someone can provide a definitive solution. I look forward to your swift responses everyone.

Stephen Rodriguez
  • 1,037
  • 1
  • 12
  • 22
  • Duplicate, duplicate.. an exact match too, found by searching the site for `"++[]"` (the stackoverflow internal search supports finding symbols when quotes are used). – user2864740 Oct 12 '14 at 09:43
  • Is this an actual problem? – Oliver Charlesworth Oct 12 '14 at 09:43
  • Stackoverflow links at bottom of that page: http://stackoverflow.com/questions/7202157/why-is-10, http://stackoverflow.com/questions/4170978/explain-why-this-works?lq=1 – Winestone Oct 12 '14 at 09:46

1 Answers1

3

You can use only six different characters to write and execute any JS code. For example here are some representations to JS constructs

false       =>  ![]
true        =>  !![]
undefined   =>  [][[]]
NaN         =>  +[![]]
0           =>  +[]
1           =>  +!+[]
2           =>  !+[]+!+[]
10          =>  [+!+[]]+[+[]]
Array       =>  []
Number      =>  +[]
String      =>  []+[]
Boolean     =>  ![]
Function    =>  []["filter"]
eval        =>  []["filter"]["constructor"]( CODE )()
window      =>  []["filter"]["constructor"]("return this")()

Proof at JS F*ck and detail at Why does ++[[]][+[]]+[+[]] return the string "10"?

Community
  • 1
  • 1
Kamran Ahmed
  • 11,809
  • 23
  • 69
  • 101