Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between != null and !== null? [duplicate]

Tags:

javascript

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?

like image 534
Nick Avatar asked Aug 16 '16 14:08

Nick


1 Answers

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).

like image 132
Pointy Avatar answered Oct 27 '22 01:10

Pointy