Thursday 20 October 2016

javascript - Why does this print out 10?





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.


Answer



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"?


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...