The original address

Have you, like me, encountered problems with the operator (==), but why?

The comparison of values depends on two factors

  • The value of the operation
  • Value type, such asString.BooleanAnd…

Another mechanism that plays an important role in value comparison is value type conversions

What are type conversions?

JavaScript being a weakly typed language, sometimes a value is converted from one type to another. This is called an implicit conversion or cast, and this can cause some errors when we use the operator (==).

The kind of comparison operator

JavaScript provides three comparison operators

  • = =
  • = = =
  • Object.is()

= =Non strict equality

  • It is known asEquality operator, will only compare two values for equality, and equality will returntrue
  • In this case, if the comparison value type is different, the value is automatically implicitly converted to a common type

The sample

console.log(1= ='1')  // true
console.log(true= ='true') // false[true in text, but false when tried]
console.log(NaN= ='NaN') // false
console.log(NaN= =NaN) // false [this is key,NaN and NaN are never equal]
console.log(-0= =0) // true
console.log(0= ='0') // true
console.log({"name": "Arwa"} = = {"name": "Arwa"}) // false[reference memory address different]

let a = {"name": "Arwa"}
let b = a
console.log(a == b) // true[reference the same memory address]
Copy the code

use= =conclusion

  • NaNNot equal to anything that contains it
  • Minus 0 is the same thing as 0
  • nullIs equal to thenullandundefined
  • Operation values can be automatically converted toString,Boolean,Number
  • StringThe type is case sensitive
  • Returns if both operation values refer to the same objecttrue, otherwise,false
  • Remember the six imaginary values (null.undefined, “ , 0 , NaN , false)

= = =2. Strict equality

  • It is becomingCongruent operator(“triple equals” or “identity”), and= =Very similar. The difference is= = =Implicit type conversions are not performed
  • Return only if the value and type of both operation values are equaltrue

The sample

console.log(1= = ='1') // false, different value types, Number vs String
console.log(true= = ='true') / / false, the same as above
console.log(true= = =true) // true, both Bollean
console.log(NaN= = =NaN) // false,NaN never equals NaN
console.log(null= = =null) // true
console.log('Ara'= = ='ara') // false, strictly case sensitive
console.log(-0= = =0) // true
console.log(null= = =undefined) // false
console.log({"name": "Arwa"} = = {"name": "Arwa"}) // false because the memory address is different
Copy the code

use= = =conclusion

  • NaNNot equal to anything that contains it
  • 0 equals 0
  • nullIs equal to thenullBut not equal toundefined
  • StringStrictly case sensitive
  • Returns if both operation values refer to the same objecttrue, otherwise,false

Object.is()

Syntax: Object.is(value 1, value 2)

  • It is called equal value and is part of the comparison operator
  • The following rules are used to check whether the values of two operations are equal

Rule 1: None of the operation values are defined (undefined)

The sample

let a
let b
Object.is(a,b) // true
Copy the code

Rule 2: Operands are strings of the same length and order

The sample

Object.is('Comparison Operators'.'Comparison Operators') // true
/ / however
Object.is('Comparison Operators'.'comparison Operators') // false, strictly case sensitive
Copy the code

Rule 3: All operation values arenull

The sample

Object.is(null.null) // true
Object.is(null.'null') // false
Copy the code

Rule 4: Operation values are objects and reference addresses are the same

The sample

let a = {"name": "Arwa"}
let b = a

Object.is(a, b) // true

Object.is({"name": "Arwa"}, {"name": "Arwa"}) // false

Object.is(window.window) // true, both references the same global variable
Copy the code

Rule 5: None of the operation values are non-0 or non-nan numbers

The sample

Object.is(1.1) // true
Copy the code

Rule 6: The operation value is +0 or -0

The sample

Object.is(0.0) // true
/ / but
Object.is(0, -0) //false, this is different from == and ===
Copy the code

Rule 7: The operation value isNaN

The sample

Object.is(NaN.NaN) // true, this is also different from == and ===
Object.is(NaN.0/0) // true
Copy the code

Note: Object.is() does not support IE, so use Polyfill instead

Small details

You know which comparison operator is used in the following function

Array.prototype.indexOf()

The answer is to use ===

More on comparison operators

If you want to learn more, there are many examples of these operators on MDN, which is very helpful for beginners!

Compare operator expressions and operators

conclusion

All three comparison operators are useful, and it is recommended to use === instead of == for some general comparisons; Object.is() is still recommended for NaN, 0, +0, -0, etc. (remember compatibility)

Why you should use Object.is() for equality comparisons

The first translation, the existence of the wrong place please correct! Will work hard to correct! 🆚