Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing to null - !== vs != in JavaScript

Ok, so I installed Linter on my Sublime editor while working on my node.js app. One of the things that it caught said that I should always use !== to compare an object to null (I usually use != ).

So I changed it...but then I noticed that the !== wasn't working.

I have this scenario:

var x = null; if (x !== null)     console.log('x is not equal to null'); 

When I use the !== the console printed that line even though it was obviously not true. When I switched it back to != it behaved normally.

So my question is, why is linter telling me to use !== if it doesn't do what I want it to...

I know I am missing something.


UPDATE Ok, so it may be a bit more complicated than I originally thought. In my real code I was using !== with the node.js GLOBAL object.

console.log('Global User: ' + GLOBAL.User);  if (GLOBAL.User != null) {     console.log('User is not null'); } 

The console line prints even when GLOBAL.User is null...

Perhaps this object is special?



Update 2

Ok, so after reading through the comments and looking at my code, I have learned that !== can have issues if the object is undefined rather than null (see this post: Why is null an object and what's the difference between null and undefined?).

So in my case, my global variable could be, depending on when this method is called, undefined, null, or full of data. I am going to go back and update my code so that it is never undefined and then !== will work consistently.

Thanks for the help!


Thanks, David

like image 989
David Avatar asked Apr 25 '13 01:04

David


People also ask

Can we compare null in JavaScript?

Comparing Equality of Null and Undefined ValuesNull and undefined values are equal when compared using the JavaScript equality operator. Use the equality operator (==) to compare if null and undefined values are equal in JavaScript.

What is the difference between null undefined and not defined?

null is an assigned value. It means nothing. undefined means a variable has been declared but not defined yet.

IS null === undefined?

It means null is equal to undefined but not identical. When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty.

Why is checking for null a good practice JavaScript?

As shown above, null is only loosely equal to itself and undefined , not to the other falsy values shown. This can be useful for checking for the absence of value — null and undefined both indicate an absence of value, thus they are loosely equal (they have the same value even though they are different types).


1 Answers

Your global.User is undefined, not null. When using == they evaluate to equal, but with === the items you are comparing need to be the same type. undefined has the type undefined and null has the type object.

undefined and null are very similar, but they generally mean two very different things. Usually undefined is the result when something has had no value assigned to it, whereas null has a value, and the value is explicitly set to "nothing".

like image 144
loganfsmyth Avatar answered Sep 19 '22 21:09

loganfsmyth