DEV Community

Kiara Contreras
Kiara Contreras

Posted on

JS == vs ===

This post expects prior knowledge on data types.

Javascript gives two options for testing equality that look really similar. Together we'll walk through the difference between == and ===.


Triple Equals / ===

Using Triple Equals tests for strict equality, meaning, both the type and the value have to be the same.

Here are some examples that return true:

10 === 10
// true : both Integers and values match

true === true
// true : both Booleans and values match

'hello world' === 'hello world'
// true : both Strings and values match

Here are some examples that return false:

100 === '100'
// false : comparison of an Integer and a String

'red' === 'blue'
// false : both Strings with different values

false === 0
// false : both different types and different value

What if we wanted to test loose equality where only the value mattered? Then we can use ==

Double Equals / ==

Using Double Equals tests for loose equality. Here only the value has to be the same. Javascript makes this possible by completing type coercion which will actually try to convert our values into a like type. Let's return to one of the previous examples that returned false.

100 == '100'
// true : comparison of an Integer and a String

false == 0
// true

The example of false == 0 can be confusing. This example returns true because 0 in Javascript is described as being falsy.

In Conclusion

When testing equality using Triple Equals is preferred. Testing the type and value ensure true equality testing.

Both experience and this article provided information on the topic and describes in detail what falsy means.

Top comments (7)

torianne02 profile image
Tori Crawford • Edited

You did a fantastic job at explaining the differences!!

My fiancé works with Python and has been dabbling in JS lately. I told him about this difference the other day and he was so amazed. Haha. It's always these small things that I think are fun to learn about!

kiarathedev profile image
Kiara Contreras

Thanks so much Victoria!

It's been great to relearn about small things like this. I feel like I can positively contribute to someone else's learning by going over basic topics and it's been fun to finally start posting.

awwsmm profile image
Andrew (he/him)

=== vs. == is how JavaScript implements equality vs. equivalence. Things that are equal are exactly the same, while things that are equivalent are more-or-less the same.

You could say that 4 and 2+2 are equal, while "four" and 4 are equivalent. The former is a mathematical equality while the latter is a linguistic equivalence.

In Java, for instance, you can use == to test for equality, which checks that the objects you're comparing are the exact same object, stored at the same address in memory. But .equals() checks for equivalence, checking that two objects' fields are equal, without caring that the two objects are in fact the exact same instance.

gypsydave5 profile image
David Wickes

Have you read about why there's a === in JavaScript? I think it's a lovely story...

Also, one of the cutest ways of pronouncing === I've ever heard: threequals 😂

kiarathedev profile image
Kiara Contreras

I haven’t! I’m always interested in tech history so I’ll definitely look into it.

Thanks! I appreciate you taking a read. I’ve just started writing posts and I’m having so much fun.

hafnch05 profile image
Christoph Hafner

Thanks for the article! Coming from Java, this was one of the first things I had do deal with when learning Javascript.

BTW: I think you got a typo on the first example of the double equals.

kiarathedev profile image
Kiara Contreras

Thanks for the read! I also appreciate the typo mention :)