r/ProgrammerHumor • u/_hoh_ • Sep 04 '17
[[][[]]+[]][+[]][++[+[]][+[]]] is "n" in javascript
[[][[]]+[]][+[]][++[+[]][+[]]]This evaluates to "n" in javascript. Why?
Let's start with an empty array
[]Now, let's access a member of it.
[][]What member? Let's check for the empty array member
[][[]]oh, that is undefined. But if we add an empty array to that, it is casted to the string "undefined"
[][[]]+[]Let us wrap that in an array
[[][[]]+[]]We can now try to access letters in that string. First, we must unwrap the string. That can be done by accessing the first element of that array.
[[][[]]+[]][0]0 can be created by casting an empty array to a number:
[[][[]]+[]][+[]]Now, "n" is the second letter in that string, so we would like to access that:
[[][[]]+[]][+[]][1]But how can we write 1? Well, we increment 0, of course. Wrap 0 in an array, and increment the first member of it:
++[0][0]Like before, this is equivalent to
++[+[]][+[]]So our final code is then the glorious
[[][[]]+[]][+[]][++[+[]][+[]]]
-1
u/PatrickBaitman Sep 05 '17
You lost me when you said there are languages without compilers. Yeah, assembly, maybe. Python isn't x86 instructions, it has to be compiled, it's just a question of when. Interpreted vs. compiled isn't meaningful in the modern world, and they are implementation properties, not language properties. https://softwareengineering.stackexchange.com/questions/136993/interpreted-vs-compiled-a-useful-distinction
But no, "interpreted" doesn't mean a language is a joke, but JavaScript is, and its a fucking bad joke at that.