Difficulty ratings range from five stars to one star.

A:

  • = = =Is the congruent (strictly equal) operator that compares whether the values and types of two variables are exactly the same
  • = =Is the equal operator, an implicit type conversion is performed before the comparison determines whether the operands are equal
1= ='1'   // true
1= = ='1'  // false
Copy the code

The = = operator

The == operator has the following rules for implicit type conversions

  • Rule 1: NaN always returns false when compared to any other type (including itself).
NaN= =NaN // false
Copy the code
  • Rule 2: Boolean is converted to Number first compared to any other type.
true= =1          // true 
true= ='2'        // change true to 1, then refer to rule 3
true= = ['1']      ['1'] = '1'
true= = ['2']      / / false, the same as above
undefined= =false // false, first false becomes 0, then refer to rule 4
null= =false      // false as above
Copy the code
  • Rule 3: Compare String and Number. First convert String to Number.
123= ='123' // true, '123' becomes 123 first
' '= =0      // true, '' becomes 0 first
Copy the code
  • Rule 4: Null == undefined is true. Otherwise, null, undefined, and any other comparison is false.
null= =undefined // true
null= =' ' // false
null= =0 // false
null= =false // false
undefined= =' ' // false
undefined= =0 // false
undefined= =false // false
Copy the code
  • Rule 5:The original typeandReference typesReference types are compared according toToPrimitiveThe rule is converted to the original type.
'[object Object]'= = {}// true, an object is compared to a string. The object gets a primitive value from toString
'1, 2, 3'= = [1.2.3] 
[1, 2, 3] returns a primitive type value through toString
Copy the code
  • Rule 6: If both are reference types, compare whether they refer to the same object
let obj1 = { name: 'lin' }
let obj2 = { name: 'lin' }
obj1 == obj2    // false

let obj3 = { name: 'lin' }
let obj4 = obj3
obj3 == obj4    // true
Copy the code

= = = operator

The congruence operator is represented by three equals signs (===) and returns true only if the two operands are equal without conversion. That is, the same type and the same value.

100= = =100   // true
100= = ='100' // false

null= = =undefined // false
Copy the code

In almost all cases, === is used to determine whether two variables are equal, and ESLint also has a related rule (eqeqeq) to apply that.

It is best to abandon == directly and use only === forever.

Read more: When is it correct to use == in JavaScript?

At the end

This is the 33rd day of alynn’s blog, delivering insight techniques. Goodbye

The last:

What is the result of “Front-end Daily Question (5)” Typeof NULL? Why is that?

Next up:

What are the data types in JavaScript?