js == === judgment and principles

Logic, etc. (==)
before the comparison, the two operator == are mandatory conversion
when converting different data types, == and! = Operator conforming to the basic principles of
1, if there is a number of Boolean operations, before the comparison for equality first converted to a value, i.e., calling Number () function

2, if one of the operands is a string, when the number of the other values, prior to the comparison of the first character string into equal value; Similarly underlayer is Number () function

3, if the operand is a target, the other operand is not, the object is invoked valueOf () method, compared with the basic type obtained in accordance with the preceding rules

The above is the principle followed in the conversion type, after conversion to the base data, the situation is different with the types and types will appear on both sides, making comparisons time and follow the following principles
1, null and undefied are equal, this to return true if js is prescribed, but in fact, the value of the underlying, undedined is derived from null, so logic, etc.

2, if there is a number of operation NaN, false is returned (NaN not associated with any other logical operator or congruent, including NaN)

3, if two operators are subject, the comparator are not the same object, it returns true, false otherwise

 

According to the above rules, then come back to take a look at a few examples of the above
undefined == false

The conversion rule 1, calling Number () method to convert it to a numeric type

 

According to the above results, it is converted into NaN == 0, 2, NaN not associated with any other logical operator, based on the comparison rule returns false

 

null == false

Number (null) == Number (false) => 0 == 0 => true // This is not acceptable

Can be output in the console is false ah? This can only be said to be a special case of js, like null == undefined, same as the following special circumstances exist

null == 0  //false

null == false //false

true == 2 // false because Number (true) returns 1

 

undefined == null 

The above mentioned, the situation is more Rule 1

 

Congruent and incongruent
it returns true in the case where two types of operands without conversion to equivalent

undefined === null

This is easy to explain, undefined and null types are different, are five kinds (after ES6 six kinds, and increased Symbol) one of the basic type, it is false

Guess you like

Origin www.cnblogs.com/nini123123/p/11240605.html