Thursday 19 September 2019

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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...