Skip to content

JavaScript Strict equality | Operator

  • by

JavaScript Strict equality Operator === checks same sides are equal in type as well as value. It checks whether its two operands are equal, returning a Boolean result.

x === y

JavaScript Strict equality

Simple example code. If the values have different types, the values are considered unequal. Otherwise, if the values have the same type and do not have numbers, they’re considered equal if they have the same value

<!DOCTYPE html>
<html>
<body>

  <script>
    console.log(1 === 1); // true

    console.log('hello' === 'hello'); // true

    console.log('1' ===  1); // false

    console.log(0 === false); // false
  </script>

</body>
</html>

Output:

JavaScript Strict equality

Which equals operator (== vs ===) should be used in JavaScript comparisons?

Answer: use strict equality operators (=== and !==) to compare two operands. it uses the Strict Equality Comparison Algorithm.

  • If the operands are of different types, return false.
  • If both operands are objects, return true only if they refer to the same object.
  • If both operands are null or both operands are undefined, return true.
  • If either operand is NaN, return false.
  • Otherwise, compare the two operand’s values:
    • Numbers must have the same numeric values. +0 and -0 are considered to be the same value.
    • Strings must have the same characters in the same order.
    • Booleans must be both true or both false.

Source: https://developer.mozilla.org/en-US/…/Strict_equality

Using strict equality with === helps in writing more predictable and less error-prone code because it checks for both value and type equality.

Do comment if you have any doubts or suggestions on this JS operator topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *