What are the reasons to use == instead of === for checking equality?

I’m wondering why you would prefer one equality check over another. I know that you would want to use == if checking a string of numerals like ‘123’ to a number 123, but are there other common scenarios to use == instead of ===?

Where would checking for equality of type as well as value be the wrong approach? What I am thinking is to always use === unless you know that == would be better. Right now it is only the ‘123’ example that I know would be a reason for using ==, and so I am looking for other common checks that would also use ==.

null == undefined // true

This is a pretty good motto to code by but I’m not sure I would say that == would ever be “better”. Rather, it might be a little more convenient in that it will do type conversions for you. But you should only rely on those type conversions if you are 100% sure that the values on both sides of the comparison are what you expect them to be. For example:

0 == '0' is true
0 == 'a' is false
0 == '' is also true!!!

I think the first two comparisons are pretty straight forward but the third one may catch you off guard and introduce a potential bug. The example @DanCouper gives is another opportunity for potential bugs.

My sense is that the majority of programmers would argue that you shouldn’t take chances with loose equality and always use strict equality. If you are going to use loose equality then you should probably memorize this table.

1 Like

Basically === does check the type of an object whereas == does not. Also, any falsy values will return true on == because the type is not checked.

As an example, take

0=='0'; //true

where both would log true. With === all would be false, as it is a ‘strict equality’.

So whenever you need strict equality checks (including the type of the object) use ===.

Thanks! I prefer to use === but I want to definitely understand that table.

Got it - thanks! Why do replies have to be 20 characters? “Got it - thanks!” wasn’t enough…

Great. Because most of <20 chars replies are “thanks” or “you’re welcome” and those reactions are summarized by pressing the  ♥ .


Oh, so instead of actually writing “Thanks!” I should just like the heart key? No need to reply is that is correct.


The reader of your code also does not have to think about the implicit coercion happening if you use strict comparison instead. So it can also help the code legibility, especially with explicit conversions written close to the condition.

Edit: But just to be clear it works both ways. Sometimes implicit can be better, for example when checking for truthy values.

if (someValue) {

Is a lot cleaner than doing a bunch of

someValue !== someFalseyValue && value !== someFalseyValue && value !== someFalseyValue

There are a few gotchas though, for example, you might not want the number 0 to be a falsy value.

console.log(Boolean(0)); // false
console.log(Boolean(1)); // true