DEV Community

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

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
 
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
 
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!