Confusing JavaScript: Double Equals or Triple Equals?

Double equals == and triple equals === are two Relational Operators in JavaScript. A relational operator is the one which shows or evaluates the relationship between two entities.

The conundrum

Although, apparently, in both equals—double == and triple ===— there is a difference of only one equal = symbol, but it mattes a lot. In this article, I shall discuss about the differences between both of them, as they confuse a lot JavaScript developers. Let us first discuss Double Equals operator in JavaScript.

Double Equals

Double equals compares two values loosely, and it also performs type coercion.

Type coercion means that the two values compared only after converting them into a common type.

It means that if one variable has the type Integer and the second variable has the type String, JavaScript will first try to guess and convert the types, either to Integer or String, and after that it will compare the contents of those two variables.

Let us take a simple example of Double Equals(==):

var a = 5;
var b = '5';
// The answer of a==b -> true

In the above example, variable a has the type Integer, and variable b has the type Character/String.

Since we have used the double equals == in the code, we would get the answer "true." But the common sense says that it is not true, as variable a has an Integer type and variable b has the String/Character type.

JavaScript first performs type coercion and then it compares the contents of the variables.

Now let us look at the Triple Equals Operator(===).

Triple Equals

Triple equals operator === follows the strict equality rules unlike Double Equals ==.

Strict equality in JavaScript means that the two variables must have to have the same datatype to yield true. Otherwise, the answer would be false.

Let us take the example of Triple Equals(===):

var a = 5;
var b = '5';
// The answer of a===b -> false

In the above example, the variable a has the datatype Integer, and the variable b has the datatype String/Character.

When we use Triple Equals Relational Operator(===) we get the expected false. As in Triple Equals case, the datatypes are first compared. If both do not have the same datatypes, the JavaScript will return false, otherwise true.


In a nutshell, in Double Equals(==) case, JavaScript will first check the datatypes of the variables, and if both of those variables do not have the same datatype, it will first decide the same datatype for both variables and then try to compare the values of those variables.

In Triple Equals(===) case, JavaScript will compare the datatypes of two variables first. If the datatypes of both variables are not equal, it will not try to change or decide the same datatype for both. Instead, it will return false right away.

I hope there will be no confusion now regarding Double equals or Triple equals. That's all for this article!



Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?