Summary
In JavaScript, the logical operator "= = =" Checks the data type of the operand first, and returns false for different data types. When the "= =" comparison is performed on different types of operands, the type conversion is then compared.
Description
Two logical operators that are judged in javascript:
- = = (equal)
- = = = (strictly equal/congruent)
Because JavaScript is a weakly typed scripting language (weakly typed), the type of two operands used for comparison allows for inconsistencies. The biggest difference between the two logical operators is the tolerance to the operand type .
That is, if the data type of the two operands is inconsistent
- "= =" attempts to convert the data type of the operand before comparing it.
- "= = =" will return false directly.
Console.log (typeofnulltypeof//object undefinedconsole.log (null null//true False
It can be seen that the results of both comparisons are different because Null is different from the undefined type.
Another example:
if (foo) { dosomething;} // the case of Foo as false: // false // 0 // "(empty string) // NULL // undefined // NaN
// and except Nan, any combination using "= =" As the result of the comparison is true, using "= = =" To compare the result is false, such as if (' = = 0) is true.
Nan is not equal to any other value, that is, if and only if A=nan, a! = A is true.
For more detailed rules on "= =" and "= = =", refer to my Evernote.
The content is primarily referenced in the JavaScript authoritative guide.
The difference between the logical operator "= =" and "= = =" in JavaScript