34

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.


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?


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.

10
  • 2
    Works for me. Commented Apr 25, 2013 at 1:13
  • 1
    Unable to repeat your experiment. My x === null when I do your code. Note that null == undefined is true. null === undefined is false. JavaScript is fun! Commented Apr 25, 2013 at 1:16
  • 1
    It shouldn't print that line. Commented Apr 25, 2013 at 1:19
  • 1
    did you clear the console (console.clear())? Maybe you are seeing a log from before Commented Apr 25, 2013 at 1:20
  • 2
    Are you sure the value you are comparing to is null and not undefined? Commented Apr 25, 2013 at 1:26

3 Answers 3

42

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

Sign up to request clarification or add additional context in comments.

2 Comments

So...if an object could be either undefined or null depending on the situation it is safer to use == rather than ===?
@David Generally I'd avoid the ambiguity and make sure it is initialized to null. As with any situation, the few possible factors you need to take into account, the easier it will be to reason about an issue.
16

The only value that doesn't equal itself in JavaScript is NaN. If null === null is false, then your JavaScript engine has serious problems ;)

To make sure your conditional statement is well written, always use the braces.

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

Comments

3

It is even more simple

var x = null;
    if (x) 6 
    if (!x) 7

the result is

undefined
7

1 Comment

Watch out for the case when x is equal to 0. It's defined, therefore NOT null, right? Still, the output is 7.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.