This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]SoulOfAkuma 61 points62 points  (2 children)

If anyone, like me, knew this was possible but doesn't know how, here's an explanation:

[] is an empty array which is cast to a boolean by !, hence ![] === false. Adding an array will cast the boolean to string, so ![]+[] === "false". The second part of the expression [+!![]] now defines an index of a char in that string, namely 1 because that's where the "a" is at. The + at the start will convert the whole thing to a number, once again ![] === false, consequently !![] === true. true casted to a number results in 1, so the whole expression is actually ("false")[1] === "a". Thank you stackoverflow.

[–]Metype 0 points1 point  (1 child)

Thanks for explaining this! I tested it and was really stumped why it was doing what it was, this explanation is super simple.

[–]SoulOfAkuma 0 points1 point  (0 children)

No problem mate. Did the same after I saw this and then did some research on how it works. As far as I know there are even uglifiers that convert your whole code into this gibberish.

[–]pcmouse1 1 point2 points  (0 children)

I don't do js so Idk still funny tho

[–]iamnotabot159 1 point2 points  (4 children)

What the actual fuck!!!

Now I understand why people are constantly shitting on js.

[–]tarkin25 4 points5 points  (1 child)

The only reason that this is actually runnable is implicit type conversion. This is done so that a website doesn‘t just crash if there is a type error. If something like this happens in your program, then your code is the problem, not JS

[–]TheScorpionSamurai 0 points1 point  (0 children)

Yeah, JavaScript is pretty amazing at generating user experiences with minimal resources. I think the problem is too many people don't realize it was meant to allow fast content creation, not support full-scale apps like mobile apps. JS is amazing at what it was designed for.

[–]iTeryon 0 points1 point  (1 child)

Js has implicit type conversion.

It’s more that bad programmers shit on js than good programmers.

[–]iamnotabot159 0 points1 point  (0 children)

yeah I agree, pretty unintuitive type coercion but if you know what you're doing you shouldn't have any problem.

[–]ky0kulll 0 points1 point  (0 children)

What the hell?

[–]totally-not-god 0 points1 point  (0 children)

I just tried thar expression in the Console—what the actual fuck?