The dreaded ‘===’ operator in JavaScript aka ‘strict equality operator’

Rishi Arora
3 min readJun 14, 2023

--

The `===` operator in JavaScript is known as the strict equality operator. It compares two values for equality without performing any type coercion. It returns `true` if the operands are strictly equal, i.e., they have the same value and the same data type. If the operands are not strictly equal, it returns `false`.

  1. Strict Equality Operator (`===`):
    When we need to compare the value and the data type of variables in javaScript.
console.log(5 === 5); // true
console.log(5 === “5”); // false

Here In the first case, both operands are the number `5` and have the same value and data type, so the result is `true`. In the second case, the operands have different types (number and string), so the result is `false`.

let a = 10;
let b = "10";
console.log(a === b); // false

In this example, the variable `a` holds the number `10`, while the variable `b` holds the string `”10"`. Since they have different data types, the strict equality operator returns `false`.

2. Loose Equality Operator (`==`):
The `==` operator is the loose equality operator in JavaScript, which performs type coercion before comparing the values. It converts the operands to a common type and then compares them. If the operands are of different types, JavaScript attempts to convert them to a common type based on certain rules before performing the comparison. This can sometimes lead to unexpected results due to implicit type conversion.

console.log(5 == 5); // true
console.log(5 == "5"); // true

In both cases, the loose equality operator converts the string ”5" to a number and compares it with the number `5`, resulting in `true`. This is an example of type coercion performed by the loose equality operator.

Some cases to consider: The loose equality operator can have unexpected results due to its type coercion rules. For example, “0” and `false` are considered equal ("0" == false` is `true`), and `null` and `undefined` are considered equal (null == undefined` is `true`). It’s generally recommended to use the strict equality operator (`===`) for more predictable and reliable comparisons.

3. Not Equal Operator (`!==`):
The `!==` operator is the strict inequality operator. It behaves similarly to the strict equality operator (`===`), but returns `true` if the operands are not strictly equal, and `false` if they are strictly equal.


console.log(5 !== 5); // false
console.log(5 !== “5”); // true

In the first case, both operands are the number `5`, so they are strictly equal, resulting in `false`. In the second case, the operands have different data types, so they are not strictly equal, resulting in `true`. The use of the strict equality operator (`===`) is generally recommended in JavaScript to avoid unexpected behavior caused by type coercion. It provides a reliable and predictable way to compare values for both equality and inequality based on both their value and data type.

--

--