The difference between Object.is and ==== and ==

  • The Object.is() method and comparison operators === and == are used to compare two values ​​for equality, but they have some differences in comparison methods and behaviors.
  • The Object.is() method is a strict equality comparison, and the === operator is also a strict equality comparison, but the == operator is an equality comparison.
  1. Strict equality comparison (===) requires that the two values ​​being compared must be exactly the same in type and value to return true.
  2. Equality comparison (==) performs type conversion, converting two values ​​​​to the same type before comparing them.
  3. The Object.is() method is more accurate for some special value comparisons:
    For comparisons between NaN and NaN, Object.is(NaN, NaN) returns true, and NaN = == NaN returns false.
    For a comparison of +0 and -0, Object.is(+0, -0) returns false, while +0 === -0 returns true

Here are some examples:

console.log(Object.is(1, 1));  // true
console.log(Object.is('foo', 'foo'));  // true
console.log(Object.is(true, true));  // true

console.log(Object.is(null, null));  // true
console.log(Object.is(undefined, undefined));  // true

console.log(Object.is(NaN, NaN));  // true
console.log(NaN === NaN);  // false

console.log(Object.is(+0, -0));  // false
console.log(+0 === -0);  // true

console.log(Object.is({
    
    }, {
    
    }));  // false
console.log({
    
    } === {
    
    });  // false

Guess you like

Origin blog.csdn.net/qq_42931285/article/details/134625757