views:

307

answers:

4

I often see JavaScript code which checks for undefined parameters etc. this way:

if (typeof input !== "undefined") {
    // do stuff
}

This seems kind of wasteful, since it involves both a type lookup and a string comparison, not to mention its verbosity. It's needed because 'undefined' could be renamed, though. My question is: How is that code any better than this approach:

if (null != input) {
    // do stuff
}

As far as I know, you can't redefine null, so it's not going to break unexpectedly. And, because of the type-coercion of the != operator, this checks for both undefined and null... which is often exactly what you want (e.g. for optional function parameters). Yet this form does not seem widespread, and it even causes JSLint to yell at you for using the evil != operator. Why is this considered bad style?

+14  A: 

typeof allows the identifier to never have been declared before. So it's safer in that regard:

if(typeof neverDeclared == "undefined") //no errors

if(neverDeclared == null) //throws ReferenceError: neverDeclared is not defined
seanmonstar
Ah, fair enough. I asked because the Google Closure library has a function called isDefAndNotNull that does this. Any idea how they get around this issue? Link: http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.source.html#line699
Derek Thurn
Whats the issue, exactly?
seanmonstar
if ((typeof neverDeclared !== "undefined") } else { return false; }
Anthony DiSanti
+3  A: 

If you are really worried about undefined being redefined, you can protect against this with some helper method like this:

function is_undefined(value) {
   var undefined_check; // instantiate a new variable which gets initialized to the real undefined value
   return value === undefined_check;
}

This works because when someone writes undefined = "foo" he only lets the name undefined reference to a new value, but he doesn't change the actual value of undefined.

Ivo Wetzel
However, you've now introduced a function call, which will harm performance.
Tim Down
I don't think that this function call will kill performance, it's much more likely that the DOM will be the bottleneck. But anyways, if you have your usual big anonymous function which contains your library whatever, you could also define `undefined_check` at the top and then just use it everywhere in your code.
Ivo Wetzel
Agreed, and I'm not saying this is a bad idea. It's just worth pointing out that calling this function will perform slower than doing a `typeof` check.
Tim Down
A: 

if (input == undefined) { ... }

works just fine. It is of course not a null comparison, but I usually find that if I need to distinguish between undefined and null, I actually rather need to distinguish between undefined and just any false value, so

else if (input) { ... }

does it.

If a program redefines 'undefined' it is really braindead anyway.

The only reason I can think of was for IE4 compatibility, it did not understand the 'undefined' keyword (which is not actually a keyword, unfortunately), but of course values could be undefined, so you had to have this:

var undefined;

and the comparison above would work just fine.

In your second example, you probably need double parentheses to make lint happy?

UniquePhoton
A: 

If the variable is declared (either with the var keyword, as a function argument, or as a global variable), I think the best way to do it is:

if (my_variable === undefined)

jQuery does it, so it's good enough for me :-)

Otherwise, you'll have to use typeof to avoid a ReferenceError.

If you expect undefined to be redefined, you could wrap your code like this:

(function(undefined){
    // undefined is now what it's supposed to be
})();
Joey Adams
If undefined has already been defined, then wouldn't you be passing it to your anonymous function through a parameter named undefined, accomplishing nothing?
Anthony DiSanti
@Anthony DiSanti: No, `undefined` is the name given to the function parameter, not its value. Nothing is passed to the function, meaning the value of the first parameter is undefined.
Joey Adams