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.
It’s amazing, what’s going on? Don’t worry, my friend, I will try to explain why.
Explaining part of the implicit conversion rules
- Convert the two compared values to the same type.
- After conversion (one or both sides of the equation may be converted), compare the values.
The comparison rules are shown in the following table: