What is the difference between ‘==’ vs ‘===’ in JavaScript?

Louis Shi
3 min readOct 28, 2018

--

Hello everyone! I hope you are having a great time. I’d like to go over some of the differences between the double equal and the triple equal in JavaScript.

To start, let’s go over the comparison for the double equal signs; we are testing for loose equality.

What does this mean? In JavaScript, double equal performs type coercion when comparing so the two values are compared after they are attempted to be converted into a common type.

As you can see above, the data type for primateOne is a string while the data type for primateTwo is a number. The double equal in JavaScript converts the two variables into a common variable first and then compares the two which is why they are equal.

What happens in the triple equal?

In a triple equal, the type coercion is not performed before they are compared so a string compared to a number will be false.

So let’s go over some further differences between the strict comparison of the ‘===’ and less strict comparison of the ‘==’. Examples of falsy values in JavaScript are “”, 0, [], and false.

As you can see the boolean values of 0, “”. false, [] are all ‘false’ which is why the double equal equates to true despite bring different data types.

The comparisons above are comparing JavaScript objects which are compared by reference rather than by value. As you can see objOne and objTwo both are empty arrays and the boolean value of both are false but even with a double equal comparison, the value is still false. The reason for that is when compared by reference they are compared by their memory location when created not by the value.

What does by reference mean? It can be better illustrated by an example of how when you change the value of one array, another array that shares the same memory location would also be manipulated.

What do you think happened to the double array?

As you can see, the double array and the manipulationArray both share the same memory space and manipulating either array will change the other one as well.

While we are discussing about datatypes and comparisons of datatypes, let’s go over very quickly the return values when you add an integer with a string and when you subtract an integer from a string.

As you can see it is very interesting to keep in mind that when in either an integer is subtracted from a string or when a string is subtracted from an integer, the return data type is an integer whereas when you add a string with an integer the return data type is now a string.

That is all I have for now, I really appreciate you for coming for the ride and hope to see you again!

--

--