r/ProgrammerHumor • • 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

[[][[]]+[]][+[]][++[+[]][+[]]]
8.1k Upvotes

367 comments sorted by

View all comments

Show parent comment

3

u/rooktakesqueen Sep 05 '17 edited Sep 05 '17

Please tell me more about how your Python interpreter "compiler" is going to reject "foo" + 7 as "bullshit"? It's going to raise a runtime error. Better, granted, than JS's behavior of returning "foo7", but it's certainly not a compile-time error. And there must be a meaningful distinction between run-time versus compile-time errors. If not, there's no meaningful distinction between static and dynamic type systems at all.

Edit: Also if we're going to be pedantic, assembly isn't machine code either and must be compiled. We usually call that tool an "assembler" but it's performing a very simplified version of the same basic tasks: lexing, parsing, outputting a binary.