javascript - Can (a== 1 && a ==2 && a==3) ever evaluate to true?

ID : 274

viewed : 89

Tags : javascriptecmascript-6javascript

Top 5 Answer for javascript - Can (a== 1 && a ==2 && a==3) ever evaluate to true?

vote vote


If you take advantage of how == works, you could simply create an object with a custom toString (or valueOf) function that changes what it returns each time it is used such that it satisfies all three conditions.

const a = {    i: 1,    toString: function () {      return a.i++;    }  }    if(a == 1 && a == 2 && a == 3) {    console.log('Hello World!');  }

The reason this works is due to the use of the loose equality operator. When using loose equality, if one of the operands is of a different type than the other, the engine will attempt to convert one to the other. In the case of an object on the left and a number on the right, it will attempt to convert the object to a number by first calling valueOf if it is callable, and failing that, it will call toString. I used toString in this case simply because it's what came to mind, valueOf would make more sense. If I instead returned a string from toString, the engine would have then attempted to convert the string to a number giving us the same end result, though with a slightly longer path.

vote vote


I couldn't resist - the other answers are undoubtedly true, but you really can't walk past the following code:

var aᅠ = 1;  var a = 2;  var ᅠa = 3;  if(aᅠ==1 && a== 2 &&ᅠa==3) {      console.log("Why hello there!")  }

Note the weird spacing in the if statement (that I copied from your question). It is the half-width Hangul (that's Korean for those not familiar) which is an Unicode space character that is not interpreted by ECMA script as a space character - this means that it is a valid character for an identifier. Therefore there are three completely different variables, one with the Hangul after the a, one with it before and the last one with just a. Replacing the space with _ for readability, the same code would look like this:

var a_ = 1;  var a = 2;  var _a = 3;  if(a_==1 && a== 2 &&_a==3) {      console.log("Why hello there!")  }

Check out the validation on Mathias' variable name validator. If that weird spacing was actually included in their question, I feel sure that it's a hint for this kind of answer.

Don't do this. Seriously.

Edit: It has come to my attention that (although not allowed to start a variable) the Zero-width joiner and Zero-width non-joiner characters are also permitted in variable names - see Obfuscating JavaScript with zero-width characters - pros and cons?.

This would look like the following:

var a= 1;  var a‍= 2; //one zero-width character  var a‍‍= 3; //two zero-width characters (or you can use the other one)  if(a==1&&a‍==2&&a‍‍==3) {      console.log("Why hello there!")  }

vote vote



var i = 0;    with({    get a() {      return ++i;    }  }) {    if (a == 1 && a == 2 && a == 3)      console.log("wohoo");  }

This uses a getter inside of a with statement to let a evaluate to three different values.

... this still does not mean this should be used in real code...

Even worse, this trick will also work with the use of ===.

  var i = 0;      with({      get a() {        return ++i;      }    }) {      if (a !== a)        console.log("yep, this is printed.");    }

vote vote


Example without getters or valueOf:

a = [1,2,3];  a.join = a.shift;  console.log(a == 1 && a == 2 && a == 3);

This works because == invokes toString which calls .join for Arrays.

Another solution, using Symbol.toPrimitive which is an ES6 equivalent of toString/valueOf:

let i = 0;  let a = { [Symbol.toPrimitive]: () => ++i };    console.log(a == 1 && a == 2 && a == 3);

vote vote


If it is asked if it is possible (not MUST), it can ask "a" to return a random number. It would be true if it generates 1, 2, and 3 sequentially.

with({    get a() {      return Math.floor(Math.random()*4);    }  }){    for(var i=0;i<1000;i++){      if (a == 1 && a == 2 && a == 3){        console.log("after " + (i+1) + " trials, it becomes true finally!!!");        break;      }    }  }

Top 3 video Explaining javascript - Can (a== 1 && a ==2 && a==3) ever evaluate to true?