Equality in JS
1. Strictly equal (===)
Strict equality is essentially a judgment of whether a value is equal to itself, and no implicit type conversion is performed before comparison. When the two values being compared are of the same type, the values are also the same, and are not of the Number type, the two values are congruent. When the two value types are Number, we need to note that NaN, NaN and NaN are not equal, +0 and -0 are congruent. In other cases, as long as the values are equal, they are congruent.
NaN === NaN //false +0 === -0 // true undefined === undefined //true null === null //true undefined === null //false ({}) === {} //false 3 === '3' //false 3 === 3 //true true === true //true false === 0 //false
2. Non-strict equality (abstract equality) (==)
Determine whether the two values compared are equal, and will be converted to the same type before comparison. After converting the type, it is consistent with the strictly equal (===) comparison rule.
Comparison Rules (A/B) | undefined | null | Number | String | Boolean | Object |
---|---|---|---|---|---|---|
undefined | true | true | false | false | false | false |
null | true | true | false | false | false | false |
Number | false | false | A === B | toNumber(B)===A | toNumber(B)===A | toPrimetive(B) == A |
String | false | false | toNumber(A)===B | A===B | toNumber(A)===toNumber(B) | toPrimetive(B) == A |
Boolean | false | false | toNumber(A)===B | toNumber(A)===toNumber(B) | A===B | toPrimetive(B) == toNumber(A) |
Object | false | false | toPrimitive(A) == B | toPrimitive(A) == B | toPrimetive(A) == toNumber(B) | A===B |
null == undefined // true null == 0 // false null == '' // false null == 'null' //false null == false // false null == [] //false null == {} //false null == NaN //false undefined == 0 //false undefined == '' //false undefined == 'undefined' //false undefined == false //false undefined == [] //false undefined == {} //false undefined == NaN //false '' == 0 //true '' == false //true '' == [] //true '' == {} // false 0 == false // true 0 == [] // true 0 == {} // false true == '1' //true true == 'true' //false true == 1 // true true == [] //false false == [] //true true == {} //false ([]) == [] //false ({}) == {} //false 3 == '3' //true NaN == NaN // false +0 == -0 // true
To summarize the rules, we can find that undefined and null consider the values equal, but when undefined and null are compared with other types equally, implicit type conversion is not performed, and it is not equal to any other type value. All objects are not equal to undefined and null, but there is a special case, such as: == undefined
and == null
All are true
Comparison of strict equality and non-strict equality can be obtained: strict equality results are more predictive, without type conversion, and are more efficient
3. The same value is equal
The same value is equal to determine whether two values are the same value.()
To judge, it is the new method of es6. A bit similar to strict equality, and does not implicitly type conversion of the two parameter values passed in, but it is not exactly the same as strict equality, and is inconsistent in terms of treatment +0, -0 and NaN.
- All undefined
- All null
- The same two strings
- All are true or all are false
- Refer to two objects with the same value
- All are NaN
- All numbers with the same value that are not NaN and not 0
- All +0 or all -0
(NaN, NaN) //true (NaN, 0 / 0) // true (+0, -0) // false (+0, +0) //true (undefined, undefined) //true (null, null) //true (undefined, null) //false ({}, {}) //false (3, '3') //false (3, 3) //true (true, true) //false (false, 0) //false
4. Zero values are equal
**Similar to the same value, +0 and -0 are considered equal**
()Implementation Plan
// Implementation plan:(Object, "is", { value: function (x, y) { if (x === y) { // You need to distinguish +0 and -0 return x !== 0 || 1 / x === 1 / y } else { // You need to distinguish NaN return x !== x && y !== y } } })
Summarize
This is the article about JavaScript equal judgment and avoiding pits. For more related content related to JavaScript equal judgment and avoiding pits, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!