The equals operator is composed of two equal signs: JavaScript is a weak type language, which means that the equals operator performs forced type conversion to compare two values.
JavaScript can be used to determine whether two values are equal.
Equal to operator
The equals operator is composed of two equal signs: =
JavaScript is a weak type language, which means that equal operators perform forced type conversion to compare two values.
"" == "0" // false0 == "" // true0 == "0" // truefalse == "false" // falsefalse == "0" // truefalse == undefined // falsefalse == null // falsenull == undefined // true" \t\r\n" == 0 // true
The above table shows forced type conversion, which is also the main cause of the use of = is widely considered to be a bad programming habit, because of its complex conversion rules, it will lead to difficult to trace problems.
In addition, forced type conversion also results in performance consumption. For example, to compare a string with a number, it must be forcibly converted to a number in advance.
Strictly equal to operator
The strictly equal operator is composed of three equal signs: =
Unlike normal equals operators, strict equals operators do not perform forced type conversion.
"" === "0" // false0 === "" // false0 === "0" // falsefalse === "false" // falsefalse === "0" // falsefalse === undefined // falsefalse === null // falsenull === undefined // false" \t\r\n" === 0 // false
The above results are clearer and helpful for code analysis. If the two operands have different types, they are definitely not equal and will also help improve performance.
Comparison object
Although the = and = operators are all equal to operators, when one of the operands is an object, the behavior is different.
{} === {}; // falsenew String('foo') === 'foo'; // falsenew Number(10) === 10; // falsevar foo = {};foo === foo; // true
Here, the operator compares not whether the values are equal, but whether they belong to the same identity. That is, only the same instance of the object is considered equal. This is a bit like a pointer in Python and C.
Note: For a more intuitive view of the difference between = and =, see JavaScript Equality Table
Conclusion
Strictly equal operators are strongly recommended. If the type needs to be converted, Explicit conversions should be performed before comparison, instead of using the complicated forced conversion rules of the language itself.
The above is the content in the JavaScript advanced series-equal and comparative content in the type. For more information, see PHP Chinese Network (www.php1.cn )!