We all know that there are two operators == and === in JS to determine whether two numbers are equal. == is not strictly equal. In the comparison process, it will first cast the comparison number to the type and then compare the value. While === is strictly equal, it will not cast the type of the value. If two numbers are of different types, then they must be different

For === this kind of strict equal comparison of words we basically won’t have what objection, but == is really silly not clear!!

Now arrange a small tips for comparison, please check ~

First, there is a special number in js -NAN, which is false when compared to any number, or HHHH, even when compared to itself

NaN == "0" //false NaN == null //false NaN == undefined //false NaN == [] //false NaN == {} //false NaN == 0 //false NaN  == false //falseCopy the code

Special, how do we know if a value is a NaN, usually by isNaN()

There are also two special numbers in js, null and undefined. These two values are equal to each other and to themselves; the rest is not equal to anyone else.

null == null  //true
null == undefined  //true
undefined == null  //true 
undefined == undefined  //true
Copy the code

The rest of the types are implicitly converted to the corresponding values for comparison, as shown in the table below:

The number B to be compared
Number String Boolean Object
Comparing several A Number A === B A === ToNumber( B ) A === ToNumber( B ) A == ToPrimitive( B )
String ToNumber( A ) === B ToNumber( A ) === ToNumber( B ) ToNumber( A ) === ToNumber( B ) ToPrimitive( B ) == A
Boolean ToNumber( A ) === B ToNumber( A ) === ToNumber( B ) ToNumber( A ) === ToNumber( B ) ToNumber( A ) == ToPrimitive( B )
Object ToPrimitive( A ) == B ToPrimitive( A ) == B ToPrimitive( A ) == ToPrimitive( B ) A === B

In the table above, ToNumber(A) tries to convert the parameter A to A number before comparing. ToPrimitive(A) converts the argument A to the Primitive(Primitive) by trying to call A’s a.tostring () and a.valueof () methods.