DEV Community

Cover image for Can (aᅠ == 1 && a == 2 && ᅠa == 3) equal true?

Can (aᅠ == 1 && a == 2 && ᅠa == 3) equal true?

Karolis Ramanauskas on April 21, 2018

Here is a JavaScript trivia question. Can the expression (aᅠ == 1 && a == 2 && ᅠa == 3) ever evaluate to true? Well, today I foun...
Collapse
 
pichardoj profile image
J. Pichardo • • Edited

Nice post, I wouldn't have thought about using an "invisible" character.

Now, for the curious people, there is also another way :)



const a = {
  valueOf: (function(){
      let x = 1;
      return () => x++;
    })()
}

console.log(a == 1 && a == 2 && a == 3); // => true

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 to true?

Collapse
 
ailrun profile image
Junyoung Clare Jang •

Yes, you also can make true from a === 1 && a === 2 && a === 3 without invisible character, since, we have getter.

let i = 0;
Object.defineProperty(window, 'a', {
  get: function() {
    return ++i;
  }
});
console.log(a === 1 && a === 2 && a === 3);
Collapse
 
pichardoj profile image
J. Pichardo •

Precisely, nice catch.

Collapse
 
superkarolis profile image
Karolis Ramanauskas •

Nice, interesting approach!

Collapse
 
nreek profile image
Henrique Marques •

Nice! Way more useful and interesting than the actual post.

Collapse
 
nestedsoftware profile image
Nested Software •

This is very cool and scary!

Collapse
 
ekeyte profile image
Eric Keyte •

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.

Collapse
 
eclejian profile image
eclejian •

Haha exactly.

Collapse
 
isaacleimgruber profile image
IsaacLeimgruber •

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?

Collapse
 
pichardoj profile image
J. Pichardo • • Edited

1,2,3 are not casted to booleans as they are primitive types themselves.

Collapse
 
isaacleimgruber profile image
IsaacLeimgruber •

In C bool is int is char

Collapse
 
theodesp profile image
Theofanis Despoudis •

Changing var to let will give you a hint

Collapse
 
superkarolis profile image
Karolis Ramanauskas •

That'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.

Collapse
 
zasuh_ profile image
Žane Suhadolnik •

Maybe with the === operator?

Collapse
 
gmartigny profile image
Guillaume Martigny •

As long as it's 3 different variables, the === operator will not be helpful. This is a "magic" trick, not a bug ;)

Collapse
 
hoz1982 profile image
Alessandro Romani •

lol nice post :)