= =,= = =Object.is()

1, the difference between

  • = =If the two values have different types, perform type conversion first and then compare
  • = = =Direct value comparison without type conversion
  • Object.is(val1, val2)Determines whether two values are the same

2,= =How do type conversions work?

  1. If the type is different, cast it
  2. Determine if you are comparingnullOr is itundefinedIf so, returntrue
  3. Check whether the type isstringornumber, if so, willstringconvertnumber
  4. Determine if either party isbooleanIf so, convert one of the parties tonumberJudging
  5. Determine if either party isobjectAnd the other party isstring,number,symbol, if so, willobjectTo determine the original type (valueOf()Methods)
  6. If one of them isNaN, directly returnsfalse
  7. If both are objects, it compares whether they refer to the same object

3,[] = =! []What is the value of?

Answer: True

The transformation steps

  1. ! The operator has the highest precedence,! []Will be converted tofalse, so at this time is[] == false
  2. According to Article 4, one of the parties isboolean,booleantonumber, so at this time is[] = = 0
  3. And then according to number five, put the array[]To the original type, call the arraytoString()Method,[].toString() = '', so at this time is' '= = 0
  4. And according to rule number three, thestringtonumber.' 'tonumberPhi is zero, so at this point0 = = 0
  5. The data type on both sides is the same0 = = 0fortrue

4, object.is () check if two values are equal

No cast is performed

  • Are allundefined
  • Are allnull
  • Are alltruefalse
  • Are strings of the same length and the same characters are arranged in the same order
  • Are the same objects (meaning each object has the same reference)
  • It’s all numbers and
    • Are all+ 0
    • Are all0
    • Are allNaN
    • Or both are non-zero and notNaNAnd are the same value