Interviewer: Can (a==1 && a==2 && a==3) Ever Evaluate to ‘true’ in JavaScript?
Recently, I was asked a very interesting interview question: Can (a== 1 && a==2 && a==3) ever evaluate to true in JavaScript?. I almost lost the job opportunity because I couldn’t answer.
At that moment, I was shocked by the question and thought the interviewer was joking.
But when I saw his “smile”, a feeling of “you must not know the answer” crossed through my mind. It was definitely not an easy problem to solve.
The article will give 6 professional answers. Let’s start right away.
Solution 1: valueOf && toString
The first solution is very simple, and I’m sure you’ll have an idea once you’ve read this code.