For js equality comparison is always a little knowledge, for some special comparison hesitancy, careful study is actually very easy to compare, record it ~

Lift to ask:

1.NaN == NaN

2.NaN === NaN

3. = = {} {}

4. = = = {} {}

5. = = [] []

6. = = = [] []

7. + 0 = = = 0

8.Number(‘1’) === 1

9.Object. Is (NaN, NaN)

10. The Object is (+ 0, 0)

11.null == undefined

12.null === undefined

13.’1′ == Number(‘1’)

Did you get all of those right? If the right do not need to read, this article mainly to solve the above problems.

False 4. false 5. false 6. false 7. True 8. True 9. True 10. False 11. True 12

The text start

Strict equality comparison (===) :

Strict equality comparison requires the comparison of whether the values are the same and whether the values are of the same type

Abstract equality comparison (==) :

The abstract equality comparison does an implicit conversion of the type before the comparison to the same type, and we just need to care if the values are the same

Object.is

Object.is() compares in the same way as ===, but with special treatment for NaN and -0 and +0. Just remember:

  1. Object.is (NaN, NaN) is true
  2. Object.is(+0, -0) is false

So if you think about it the other way around === and === are the opposite, hahaha

Answer the above question:

  • For NaN comparisons, either == or === is false. NaN is used to represent the solution to some poorly defined mathematical problem: 1+ infinity and 2+ infinity are NaN, for example. The congruence operator assumes that NaN is not congruent with any other value, including itself. The equation (x! == x) is true only if the value of x is NaN. So 1,2 are both false

  • The comparison of objects and arrays, although they appear to have the same type and the same value (both empty), but reference types like objects and arrays, they are compared through memory calls, and determine whether their memory addresses point to the same address. So object and array comparisons 3,4,5,6 are all false

  • Floating-point 0 is neither positive nor negative. The distinction between +0 and -0 is necessary to solve certain mathematical problems, but for the most part we don’t care. The congruence operator considers these two values to be congruent. So only special processing is done in object. is. 7 is true and 10 is false

  • On null and undefined comparisons. The JavaScript specification states that null and undefined cannot be converted to any other value until equality is compared, and that null and undefined are equal. Both null and undefined represent invalid values. Null = = is undefined. But null and undefined are different data types, so null! == undefined. So 11 is true and 12 is false.

  • Number(‘1’) === 1 and Number(‘1’) == 1

    Number(‘1’) === 1: Converts string 1 to Number, so Number(‘1’) and 1 have the same type and value, so congruence is true

    ‘1’ == Number(‘1’) : a comparison between a string and a Number type, but == does not compare types, only values are the same, so it is true.

conclusion

  • Remember what you need to remember:

    1. Object.is (NaN, NaN) is true
    2. Object.is(+0, -0) is false
    3. null == undefined
    4. NaN ! == NaN / NaN ! = NaN
    5. +0 == -0 / +0 === -0
  • Objects and arrays that refer to types of data, you have to look at the address of the reference, and different objects or arrays they refer to different addresses, so they don’t vary

  • === is false as long as the data type is different

All right, that’s it

Thank you for your comments if you have any questions

If it is helpful to you, please like, follow and bookmark it