0%

javascript-strict-equality

How === works ?

Suppose the following expression

1
A === B

How strict equality works in JavaScript? Here are the steps:

  1. If the operands are of different types, return false
  2. If both operands are objects, return true only if they refer to the same object.
  3. If both operands are null or both operands are undefined. return true.
  4. If either operand is NaN, return false
  5. Otherwise, compare the two operand’s value as below
    1. Numbers must have the same numeric values.
    2. Strings must have the same characters in the same order.
    3. Booleans must be both true or both false.

Code Example

1
2
3
4
5
6
7
8
9
console.log(1 === '1'); // false, different type.
console.log({ name: 'zdd' } === { name: 'zdd' }); // false, different object with same value.
console.log([] === []); // false
console.log({} === {}); // false
console.log(null === undefined); // false, different type.

// Pay more attention to the following examples
console.log(NaN === NaN); // false
console.log(+0 === -0); // true

References

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality