Equality (==) vs Strict Equality (===) in JavaScript

Rajesh Chittampally
3 min readFeb 22, 2021
Image by author

In a program, many times we want to compare whether two values are same or not.

In JavaScript, we can check if two values are equal or not in mainly two different ways. They are:

  • Equality (==)
  • Strict Equality (===)

Equality (==):

Equality operator will first perform type conversion by converting two values into same type and then will compare those two values. If the two values are equal then it will return true otherwise it will return false.

Strict Equality (===):

Strict Equality operator will not perform type conversion. It will check whether the two values are of same value and of same type. If they are of same type and have same value, it will return true otherwise it will return false.

Behind the scenes:

To understand how these two will work behind the scenes and how they will compare two values, we’ll consider an example of two apples. One red-colored apple and the other one of the color green.

The red-colored apple is on the left-hand side and green colored apple is on the right-hand side.

Photo by an_vision on Unsplash
Photo by Immo Wegmann on Unsplash

When you use the equality (==) operator to compare two values, then JavaScript will first change the color of green-colored apple to red. Then it will compare two red-colored apples to check whether they both are equal or not.

Since now both are apples and both are of the color red, it will return true.

On the other hand when you use strict equality (===) operator to compare two values, then JavaScript will not change the color of green-colored apple to red.

It will directly compare the two apples and check whether they both are of the same type and have the same value.

Here, since one apple is red color and the other is green color, JavaScript will return false.

Wait, there’s still more to know…

A gist to see how equality operator works with different data types in JavaScript.

A gist to see how the strict equality operator will work with different data types in JavaScript.

As you can see in the above two gists, equality operator (==) will work as expected with other data types but it will behave differently when dealing with objects.

This is because objects in JavaScript have a complex structure when compared with other data types.

When you use equality (==) or strict equality (===) operator, while comparing two objects, JavaScript will check whether the objects are identical or not (i.e., whether the two objects are referring to the same memory location). Only if the two objects are identical, it will return true otherwise it will return false as shown in the above gists.

This different behavior of JavaScript with objects should be kept in mind while working with them. Otherwise, it may lead to serious problems.

--

--

Rajesh Chittampally

#reacting to the challenges faced by users and developing solutions with the help of #php, #node, #mysql so that users can #vue the applications as intended