Here is a JavaScript trivia question. Can the expression (aᅠ == 1 && a == 2 && ᅠa == 3)
ever evaluate to true
? Well, today I found out it can. Run the below code and see for yourself.
Now let's look at seemingly the same code below. Try to run it. This time the expression does not evaluate to true
. So what changed?
The difference is actally really simple and much less mysterious once you figure out what's happening. Apparently, there is a Unicode character called Halfwidth Hangul Filler which is a Korean symbol and is, for all intents and purposes, invisible. So the reason why the first code snippet evaluates to true
is because the three variables a
are in fact three distinct variables. One of them is prepended with halfwidth hangul filler, another one is left intact and the third one is appended with the hidden character.
Now next time someone asks you whether (aᅠ == 1 && a == 2 && ᅠa == 3)
can ever equal to true
, you can answer them that yes, indeed it can (with a small caveat) 😀
Top comments (16)
Nice post, I wouldn't have thought about using an "invisible" character.
Now, for the curious people, there is also another way :)
Just for reference, the concepts used here are type coercion, closures and immediate function invocation.
Regards
p.s. can
a === 1 && a === 2 && a === 3
evaluate totrue
?Yes, you also can make true from
a === 1 && a === 2 && a === 3
without invisible character, since, we have getter.Precisely, nice catch.
Nice, interesting approach!
Nice! Way more useful and interesting than the actual post.
This is very cool and scary!
This feels a little clickbait-y. It feels the same as photographing two glasses of water after 3 hours in the freezer, and then saying, “we put two glasses of water in the freezer and one of them didn’t freeze!” And then revealing that one glass was actually full of vodka.
I don’t want to try to misunderstand the spirit of this article, but I don’t get why any of this is relevant.
Haha exactly.
Changing
var
tolet
will give you a hintThat's right! Although for the hint to be useful you have to know that
let
throws an exception if you try to redeclare a variable.Does it not work if you set a to True since then 1,2,3 are cast to booleans and are evaluated at True aswell?
1,2,3
are not casted to booleans as they are primitive types themselves.In C bool is int is char
lol nice post :)
Maybe with the === operator?
As long as it's 3 different variables, the
===
operator will not be helpful. This is a "magic" trick, not a bug ;)