Raymon Schouwenaar

How to check equality of values in JavaScript!

Written by Raymon on , time to read ~2 minutes.

In JavaScript, there are 2 comparison operators. The triple equals and the double equals (=== or ==).

When I checked JavaScript in an older application, I saw a lot of double equals in it. I thought that couldn’t be right.

Then I realized it was built by a few Java developers. If you check the documentation of Java Equality, Relational and Conditional Operators you soon find out why it was written like that.

In Java, there are only double equals operators (==). So there was my “aha” moment.

Avoid the double equals (==) operator in JavaScript

There are a lot of articles written about how you should check for equality in JavaScript. Mostly to long and harder to understand for beginners with JavaScript. So let me make it easy for you.

Double equality (==) operator

The == operator will start with checking the type of the value. If the type is not equal, it’s gonna check the value. If the value is equal it will return true, otherwise false. Like in this example:

How works == underwater

The type or value needs to equal to get a result of true, otherwise, it will be false.

Here is an example of how the double equality operator works underwater:

So only the value is checked. But the first value is a type number and the second is a string. So if you want to have the reliable code, the double equality operator does not what you would expect (except if you already knew this).

Triple equality (===) operator

The === starts with checking type equality, just like the == operator. If it is equal it will check the value equality, if not it will be false. If the type and value are equal, it will be true. Like this example:

How works === underwater

The type and value need to equal to get a result of true, otherwise, it will be false.

Here is an example of how the triple equality operator works underwater:

Use the triple equals (===) operator in JavaScript

Hopefully, by now, you understand why you need to use the triple equals operator (===) in JavaScript instead of the double equals operator (==).

Using the triple equals operator will also make your code and checkings in it more reliable. Other developers can trust that what you expect is definitely true because you compare the value and type of the value.

Hopefully, this makes it all clear, if not please ask your questions in the comments, I’m happy to help you out!

comments powered by Disqus


CSS Grid Layout Metro design blocks

The times have come that we don’t have to fiddle around with Floats and Tables anymore! Flexbox is already an established term in the industry of Frontend Developers.

3 ways to loop over Object properties with Vanilla JavaScript

It happens a lot that you need to loop over an Array with JavaScript Objects! But sometimes you just don’t know what kind of properties that Object has. Lucky we are that JavaScript offers a few ways of looping over JavaScript Object properties.