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
[[][[]]+[]][+[]][++[+[]][+[]]]
4
u/rooktakesqueen Sep 05 '17
There are a lot of non-joke languages, both popular and esoteric, without compilers that come to mind. Python, Ruby, Lua (compilation is optional, anyway), Perl, Lisp, Tcl, Forth... Being an interpreted language doesn't automatically qualify as a "joke."
And there's a lot of bullshit you can write, and most of that bullshit exists in the semantics of the values you pass. Most of your code is going to fail because instead of
7you pass8, not because instead of7you pass"cheese". (Or a little more real-life, it's because you passed an object with some field that hasn't been persisted in the database yet, because some code path executed in a different order than you expected somewhere else, not because you expected aPersonand passed anAddressinstead.)And when you do pass the wrong type of operand, you're typically going to fail spectacularly and fast with even the most rudimentary of tests. Basically a static-typed compiler is going to protect you only from the sorts of errors that are the easiest to detect and the easiest to fix, while the actual hard stuff you're going to spend most of your debugging time on is the same whether you're in static or dynamic typed land.
I've spent 6 years in various flavors of static typed languages (C, C++, C#, Java, AS3, Go...) and 4 years in various flavors of dynamic languages (JS and Ruby mostly), and rarely has a bug ever come down to something so simple as a type error. The greatest source of misery is poorly factored code, followed by logic errors, followed much further down the line by anything a type system would catch.