How to check equality of values in JavaScript!

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!

  • Jasper Seinhorst

    Goed geschreven artikel, maar ik zie een onwaarheid bij een code-example. Double equals is zeker niet te schrijven en hetzelfde als “if(typeof 5 == typeof ‘5’ || 5 == ‘5’) {” Dit voorbeeld zou namelijk betekenen dat “jasper” == “raymon” waar zou zijn, want typeof is bij beide string. Bij double equals is juist de essentie dat er geen type check wordt gedaan. Deze zou dus weggelaten moeten worden in het voorbeeld.

    Correct voorbeeld zou zijn bij double equals if (5 == ‘5’) en bij triple equals if (typeof 5 == typeof ‘5’ && 5 == ‘5’)

    • Sorry voor mijn late reactie! Je hebt helemaal gelijk! Ik ga het aanpassen 🙏