In JavaScript, the == and == operators can be used to determine whether the two values are equal; the difference is that if the two values that are judged are inconsistent, the === operator will directly return false, while the == operator will make a judgment after the type conversion. The detailed judgment rules are as follows:
===Rules for judging operators
1. If the types of the two values are inconsistent, return false.
2. If the two values have the same type and the values are the same, return true. NaN is a special case, NaN===NaN returns false.
3. If both values are of object type, then like Java, unless the references are consistent (the reference points to the same object address), even if the content in the object is exactly the same, the two values are considered inconsistent, and the corresponding operation will return false. For example, create two new arrays with exactly the same content, and then perform the === operation on them and return the result to false - although their content is exactly the same, they still belong to two different objects.
4.0===-0 returns true.
==Rules for judgment of operators
The == operator will type the value and then compare it. The type conversion follows the following principles: first convert it to number and then compare it, and first convert it to string and then compare it. The specific judgment rules are as follows:
1. If the two value types are the same, return after performing the === operation.
2.null==undefined is true.
3. True will be converted to 1 and compared, false will be converted to 0 and compared.
4. If one of the values is an object, convert it to number and then compare it, except for the Date object.
5. If one of the values is a Date object, convert it to string and then compare it.
experiment
The code copy is as follows:
console.log("3" === 3);//false
console.log(NaN === NaN);//false
var a = {x:1, y:2};
var b = {x:1, y:2};
var c = a;
console.log(a === b);//false
console.log(a === c);//true
console.log(0 === -0);//true
console.log("3" == 3);//true
console.log(null == undefined);//true
console.log(true == 1);//true
console.log(true == 9);//false
console.log([9] == 9);//true
console.log([9] == "9");//true
var d = new Date();
var s = d.toString();
var n = d.valueOf();
console.log(d == s);//true
console.log(d == n);//false