Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)
edited by

Is it ever possible that (a== 1 && a ==2 && a==3) could evaluate to true in JavaScript?

This is an interview question asked by a major tech company. It happened two weeks back, but I'm still trying to find the answer. I know we never write such code in our day-to-day job, but I'm curious.

1 Answer

0 votes
by (106k points)
edited by

Yes, the statement (a== 1 && a ==2 && a==3) can be evaluated to true by using the method toString that changes what it returns each time it is used such that it satisfies all three conditions. See the code below:-

const b = { 

i: 1, 

toString: function () { 

return b.i++; 

  } 

if(b == 1 && b == 2 && b == 3) {

console.log('Hello World!');

}

image

Browse Categories

...