Single Equal (=) vs. Double Equals (==) vs. Triple Equals (===) in JavaScript

In JavaScript, any new learner or even an experienced programmer could get confused in these multiple types of equal signs. In this article, we will explain you difference between these various types of equal signs.

1. Single Equal (=) in JavaScript

We use Single Equal sign to assign value to the variable or to initialize an object. For example.

 
//Variable assignment
var amount = 100;
//Object initialization
var t = new Date();
 

In the above code, you can see that the value 100 is stored in the amount variable using a single equal (=) sign. In the same way, we have created a new Date() object and saved it into a variable named t.

Single Equal sign CAN’T be use for comparison.

2. Double Equals (==) in JavaScript

Double Equal sign is use for comparison. Suppose, in the if statement, you want to compare two values or objects, then you can use Double Equal sign. For Example.

 
if(20 == 20) {
    alert(true);
}
else {
    alert(false);
}
 

The output of above code is True. Because Number 20 is matched.

BUT, you should avoid using Double Equal Sign. Why? Let’s see in the below example.

 
if(20 == "20") {
    alert(true);
}
else {
    alert(false);
}
 

The output of above code is True.

But it is wrong! Because the left side value 20 is a number and right side value “20” is a string. When you use Double Equals, JavaScript attempts to convert types. In the above case, it has converted the number to the string, and both values get matched (“20” == “20”), and eventually, the result is True.

When we compare values or objects, we don’t want the type to get change. We always compare the same types. If accidentally value gets changed, then it will be confusing.

NOT Equal (!=)

Opposite of Double Equals (==) is NOT Equal (!=), but the behavior is the same. NOT Equals also converts type while comparing. Let’s see the above example with NOT Equal (!=).

 
if(20 != "20") {
    alert(true);
}
else {
    alert(false);
}
 

As expected, it converts types, and that’s why the output is False, which is not right when we are comparing numbers with string. In the above case, number 20 gets converted into a string, and because we have used NOT Equals sign, the result (“20” != “20”) is False.

Double Equal (==) and NOT Equal (!=) have a better alternative. Let’s look at it.

3. Triple Equals (===) in JavaScript

Triple Equals are best to use for comparisons. Like Double Equals, they DON’T convert types while comparing values or objects. Let’s see the same examples with Triple Equals, which we have used for Double Equals.

 
if(20 === "20") {
    alert(true);
}
else {
    alert(false);
}
 

Output of above example is False.

This is right. That’s what we have expected from the comparison sign should do.

NOT Equals (!==)

Opposite of Triple Equals (===) is NOT Equal (!==), but the behavior is the same. NOT Equal does not convert type while comparing. Let’s use the same example we have used for NOT Equals with Double Equals.

 
if(20 !== "20") {
    alert(true);
}
else {
    alert(false);
}
 

The output of the above example is True. In the above case, there is no conversion happened; that’s why a straightforward answer to the above comparison is True.

Note: Always use Triple Equals (===) and NOT Equals (!==) for comparisons.

Below is the table representation of all the compare signs explained above. It will be easy for a glance.

Equal Signs in JavaScript Table Overview

SignExample Output
Double Equals (==)20 == “20”True
Not Equals (!=)20 != “20”False
Triple Equals (===)20 === “20”False
Not Equals (!==)20 !== “20”True

That’s it. We hope you understood how these equal signs work in JavaScript.

Categories js

Subscribe Now!

Subscribe Us For Latest Articles