background

One day at work, my next-door colleague Huang typed console.log(a == 1&& a == 2&& a == 3) in the Chrome console and asked me, as the cursor flashed, if you could define a variable a that would print true.

I thought, this guy doesn’t work well, fiddles around here, and makes such a long and somewhat bizarre expression for me to guess, must be trying to test me, and when I can’t solve the honest me laugh.

At this time around a few colleagues also came around to watch, at this time MY heart is very nervous, forehead out of small sweat, eyes staring at the screen of the topic, but the mind is really not out of the solution by colleagues laugh at the scene.

After I calmed down, I gradually found the clue of this problem, which used non-strict equality (==), my mind suddenly jumped, thinking that in non-strict equality comparison, different types of variables will be implicitly converted. Thinking of this, I gently raised the corners of my mouth, cleared my throat, and began to JJYY to the people around me.

Abstract Equality Comparison

So let’s look at non-strict equality.

When comparing two variables with non-strict equality, implicit type conversions are performed in the case of type inconsistencies. The transformation rules are shown below.

Here 2, 3, 6 and 12 are the four special scenarios.

The rest follow the following logic.

After a series of conversions, it becomes a strict equality of the same type for comparison.

ToPrimitive & ToNumber

Above, the more important are ToPrimitive and ToNumber, let’s learn more about how they are defined.

ToPrimitive

ToNumber

There is not much to learn about ToNumber. Just look at a picture and see what it feels like.

The last

Log = () => true; “And then quietly returned to work on the station.