Equality and comparison
JavaScript has two ways of determining whether two values are equal.
equals operator
The equals operator consists of two equals: = =
JavaScript is a weakly typed language, which means that the equals operator enforces type conversions to compare two values.
"" = = "0"//False
0 = ""//True
0 = "0"//True
False = = "false"//False
false = = "0"//True
false = = undefined//False
false = = NULL//False
NULL = = undefined//True
"\t\r\n" = 0//True
The table above shows strongly typed conversions, which is the main reason why using = = is widely regarded as a bad programming habit.
Because of its complex conversion rules, it can cause problems that are difficult to track.
In addition, coercion of type conversions can also result in performance consumption, such as a string that, in order to be compared to an array, must be cast to a number beforehand.
Strictly equals operator
Strictly equal to the operator consists of three equals: = = =
Do not want the normal equals operator, strictly equal to the operator will not be forced type conversion.
"" = = = "0"//False
0 = = ""//False
0 = = "0"//False
false = = = "false"//False
false = = = "0"//False
false = = = undefined//False
false = = = NULL//False
NULL = = undefined//False
"\t\r\n" = = 0//False
The results above are clearer and helpful for code analysis. If the two operand types are different, it is certainly not equal and can also contribute to performance improvement.
Compare objects
Although the = = and = = operators are equal to operators, the behavior is different when one of the operands is an object.
{} === {}; False
New String (' foo ') = = ' Foo '; False
New Number (10) = = 10; False
var foo = {};
Foo = = = = Foo; True
Here equals the operator comparing whether the value is equal, but whether it belongs to the same identity, that is to say, only the same instance of the object is considered equal.
It's kind of like a pointer comparison in the IS and C in Python.
Conclusion
It is strongly recommended to use strict equals operator. If the type requires conversion, the explicit conversion should precede the comparison.
Rather than using the complex cast rules of the language itself.