enjoying salad since 1978.

Sunday, February 17, 2008

JavaScript undefined vs. null

I was reading a modern, popular book on JavaScript last night and was disappointed by the handling of null. The author started out doing a lot of checking like:

if (foo == null) {   alert('foo is not set.'); }

Then told the reader that they could just remove the == null because javascript knows you mean "== null"

What?! This isn't why you don't check for equality with null. It's because foo == null doesn't even remotely do what most people think it does in this context.

It's a commonly held belief that uninitialized properties in JavaScript are set to null as default values. People believe this mostly for 2 reasons: 1) foo == null returns true if foo is undefined and 2) authors don't teach JavaScript properly.

A property, when it has no definition, is undefined. Put that way, it's pretty obvious.

null is an object. It's type is null. undefined is not an object, it's type is undefined. That part is less obvious.

The real trouble is that == does type coercion. === checks for both type and value and is the most intuitive form of equality in JavaScript, in my opinion.

I fired up a Jash console to hopefully clear things up for you.

>> window.hello
null
>> window.hello.something
window.hello has no properties
>> window.hello == null
true
>> window.hello === null
false
>> window.hello === undefined
true
>> if (window.hello) { alert('truthy'); } else { alert('falsy'); } // will print falsy.
null
>> window.hello == undefined
true
>> null == undefined
true // there's the rub, sir.
>> null
null
>> undefined
null
>> typeof null
object
>> typeof undefined
undefined

So people write

if (foo == null) {
   foo = "Joe";
}
When what they really mean is
if (!foo) {
   foo = "Joe";
}
If you find yourself with a lot of null checks in your JavaScript, set aside some time and watch Douglas Crockford's "The JavaScript Programming Language" talk on Yahoo Video. It's part 1 of a 3-part series of excellent and enlightening talks.

6 Comments:

Blogger Phoenix said...

A nice help this post was.
Thx for your time.

10:18 AM

 
Blogger Sean said...

Yeah...but your suggested replacement is more ambiguous.

if(!foo){

would be triggered by undefined, null or false.

var bool = false;
alert(!bool);
alert(bool == null);

doing a null check is more specific and tells anyone reading the code that js is looking for 'non value' variables

11:29 PM

 
Blogger David said...

Unfortunately, your session with Jash is rendered pretty hilarious by the fact that evaluating "window.hello" produces "null". What possible reason is there for that? Oh, wait, the source code for Jash basically says the equivalent of if (result == null) print("null")! (Of course, evaluating alert(window.hello) in a browser will say "undefined".)

12:56 AM

 
Blogger LudoA said...

Hi!
Would you mind saying which book it is? Just so I know which to avoid should I buy a JS book.

Either way, thanks for the informative post!

2:46 AM

 
Blogger Steve Jenson said...

@david: truly unfortunate.

9:08 AM

 
Blogger Neha Chachra said...

I totally agree about the Douglas Crockford javascript series. Though I forgot some of it, so your blog was useful to verify that I remembered correctly :)
Thanks :)

12:20 PM

 

Post a Comment

Links to this post:

Create a Link

<< Home