The equality (==) and strict equality (===) operators in JavaScript

Charles Ivia
3 min readNov 28, 2021

If you have written a substantial amount of JavaScript, you have encountered a bug or two caused by the equality operator. Remember that time you compared a form input to a number and got true?

So, what the heck is the difference between == and === ?

In JavaScript, two kinds of comparison operators exist — the equality (==) and strict equality (===) operators. While they may look similar, the operators work in disparate ways. Let us see what the differences are, shall we?

The Equality Operator

The equality operator, also known as the type-converting operator (==) checks whether two operands are equal and returns a Boolean result. When used, the equality operator attempts to convert operands from one type to another. As a result, operands may be considered equal even when they are of different types. This behavior can cause some confusion even for more seasoned developers.

The equality operator uses the Abstract Equality Comparison Algorithm (AECA) and works as follows:

1. If both operands are objects and reference the same object, return true.

2. If either of the operands is undefined and the other is null, return true.

3. If one operand is a number and the other is a string, attempt to convert the string to a number.

4. If one of the operands is a Boolean, convert the Boolean operand to 1 if it is true and to 0 if it is false.

5. If one of the operands is an object and the other is a number or a string, try to convert the object to a primitive using the object’s valueOf() and toString() methods.

6. If both operands are strings, return true only if both operands have the same characters in the same order.

7. If both operands are a number, return true only if both operands have the same value. Note that +0 and -0 are treated as the same value. If either operand is NaN, return false.

8. Lastly, Boolean operands return true only if operands are both true or both false.

Let us explore a few examples:

console.log( 1== true); // true

console.log(0 == false); // true

console.log(“ ” == 0); // true

console.log( 1 == “1” ); // true

console.log(“str” == “str); // true

console.log( 0 == null); //false

console.log(0 == undefined); // false

console.log(null == undefined); //true

let myPet = {name : “Tommy”}

let yourPet = {name: “Tommy”}

console.log(myPet == yourPet); //false

console.log(myPet == myPet); //true

The Strict Equality Operator

The strict equality or the identity operator checks whether two operands are equal. It does not perform type conversion. It thus considers operands of different types to be strictly different. As such, when comparing two values of different types, the result is always false.

The operator === uses the Strict Equality Comparison Algorithm (SECA) and works as follows:

1. For numbers, if both operands have the same value but no NaN, it returns true.

2. If both operands are strings and have identical characters in the same order, it returns true.

3. For Booleans, if both operands are true or are both false, it returns true.

4. For objects, if both operands reference the same object, it returns true.

In general, if the operands are different types, then the result is always false.

Let us see some examples:

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

console.log(“str”===”str”); //true

let myPet = {name : “Tommy”}

let yourPet = {name: “Tommy”}

console.log(myPet === yourPet); //false

The last example returns false since the operands reference different objects.

Wrapping up

It is worth noting that NaN in both the equality and strict equality operator compared with any other value will always return false.

Because the equality operator (==) makes type conversions, some comparisons may have unexpected results. Therefore, it is recommended you use the strict equality operator (===).

Thank you for reading this post. Any additions or comments are welcome in the comment section.

--

--