JavaScript

Difference between == and === in JavaScript

January 15, 2022

Difference between == and === in JavaScript

In this article, we’ll learn the difference between == and === in JavaScript, as well as use cases for each of the variations of this comparison operator.

Hey you  programmer, okay? Let’s learn something new!

This is a question that can plague those who are starting programming and also those who are migrating to JavaScript

Because in most languages we don’t have these two comparators, just one that compares the equality

So let’s go to the definition of each one:

== (comparing values): The == compares only if the result is equal on both sides;

See some examples:

Note that even if we compare a string that represents the same int value, 1 == ‘1’, we have true

Because the == only checks the values

And also if we compare 1 == true, it also checks for true, because the 1 in the programming represents true too.

=== (compares identical values): An identical value can be defined as type and equal value

That is, if we compare 1 === to ‘1’, we will have false, because ‘1’ is a string and 1 is an int, or number (as the number type is called in JavaScript)

So see some examples:

Note that we now force JavaScript to check the type in addition to the value that was already checked with ==

Remembering that it is always safer to use ===, == can generate some unexpected failures

Precisely for being a little more liberal in relation to ===

Conclusion

In this article we learned  the difference between == and === in JavaScript

There are two comparison operators, but with the == we only compare the equality of values

In ===, the identical operator, we compare the type and value to see if they are equal.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x