== v/s === in JavaScript

The difference between double equals and triple equals in JavaScript

ยท

3 min read

Table of contents

No heading

No headings in the article.

The first thing to be clear about is that a single equal sign doesn't mean we are comparing two numbers or variables. In programming, a single equal-to implies assignment. In simple terms, we need a symbol to assign a value to a variable. So, we use a single equal-to for assigning a value to a variable.

For example -

a = 53 ;

b = "Coding" ;

Dhoni = 7 ;

Messi = "Footballer" ;

Here,
a has a value of 53,
b has a value of "Coding",
Dhoni has a value of 7,
Messi has a value of "Footballer".

So, make it clear in your mind that a single equal sign means it is used to assign something and, it is called an assignment operator.

So, you don't have any confusion with the = sign.

Now, let's come to the == v/s === part.

I won't go with many technical terms and jargon rather I will use the simplest words and explain them very simply so that you can have a clear understanding of the two.

To read more about it, you can use the mdn docs.

We can say that one of the two signs means strictly equal and the other is loosely equal.
This might sound confusing. Let's understand them a bit more elaborately. There is nothing much to understand and it is very simple actually.

== (double equals) means loose equality.

=== (triple equals) means strict equality.

For example -

const variable1 = 125;
const variable2 = "125";

Now, let's compare them.

console.log(variable1 == variable2);

This will return true as output.

and,

console.log(variable1 === variable2);

Apparently, this should also return true. But, it will return false.

I will attach an image showing the output of the codes. You can also try this on the browser console by yourself.

Let us understand the logic behind the respective outputs.

When we compare two values with the double equals ( == ), the values are compared irrespective of the data types. So, in the case above, variable1 has a value of data type Number and, variable2 has a value of data type String. Although both the values are the same, their data types are different.
So, when variable1 and variable2 are compared with double equals ( == ), it doesn't check for their data types but it only checks if the values are equal or not.

For console.log(variable1 == variable2); , the values are the same that is, 125 and thus, it will return true.

Now, when we compare the two values with the triple equals ( === ), the values as well as their data types are checked and compared. As we have discussed above, variable1 is of data type Number, and variable2 is of data type String. So, although both the values are the same in values, their data types are different.

For console.log(variable1 === variable2); , the values are the same that is, 125 but as the data types are different and hence, it will return false.

I am attaching this image below for your reference ๐Ÿ‘‡๐Ÿป

If you want to read more about it, please go through the mdn docs to understand the concepts with more depth.

I hope I was able to explain the difference between the two and their respective uses.

Thank You โค๏ธ

ย