Sorry for what I'm sure had have been asked in the past, but it's very hard to search for a question like this. "!=" and "!==" are not exactly search friendly. If anyone knows a duplicate question you can point me to it.
What is the difference between doing myVar != null
and myVar !== null
?
I know that !=
is not equal
and !==
is not equal value or not equal type
, but when comparing to null is there ever a case where they would return different results? Is one better to use than the other?
The answer to the specific question about whether there's ever a case where !=
and !==
comparisons involving null
get different answers is yes:
undefined != null // false
undefined !== null // true
The rules for ==
and !=
explicitly include a clause that stipulates that null
and undefined
are the same.
Personally — that is, in my code — that fact is a reason for using !=
(or ==
) when checking for null
in cases where undefined
should be treated the same way (which is a pretty common situation).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With