JavaScript: typeof “undefined” vs undefined

As far as I can tell, there are two ways to determine if a variable is not defined. Value and type.

Checking the value. This might be the most common way, and the best in most cases when you know what you are checking. Take this example:

var a;
alert(a === undefined);

Simple enough, you have a variable that is not defined, but it DOES exist, and you are comparing it against a global variable “undefined’, that is not defined but also exists, and ultimately, can be broken and not give what you expect. Take this example:

var a, undefined = "defined!";
alert(a === undefined); // woah, not what we expected

Be careful when checking against the global variable “undefined”, it may not be what you think it is. This might seem silly, but when creating a library, that may possibly be used with other libraries, that you have no control over, you have to make sure you have control over your stuff, and you cannot be broken by others. There are ways around this. Take this example:

var undef, a;
alert(a === undef); // woah, not what we expected

All I am doing here is creating a variable called undef, that I KNOW is not defined, and checking the values against it. This is how processing.js currently checks for undefined.

Another potential issue. What if you need to check to see if a variable is defined, but you didn’t create it. Maybe you want to see if another library has created it. Take this example:

//var a;
alert(a === undefined); // now we're broken

a does not exist. We didn’t create it, so why should it? So we obviously don’t know what we are doing and the browser is nice enough to stop doing our silly commands.

A way around this is to check the type, and not the value:

//var a;
alert(typeof a === "undefined"); // we are fine and can keep on rolling

A ever so slight improvement on that last one is like this:

//var a;
var undef = "undefined";
alert(typeof a === undef );

So now, if we are checking like this a lot, we don’t have to keep creating and throwing away a single string. We create it once, and re use it.

One argument I see is why check its type against “undefined”, if you want to see if something exists, and plan on using it, you should probably know its type before you use it. So something like:

var func = "function";
if (typeof a === func) {
    a();
}

Why would you care if a variable exists or not if you don’t intend to use it, and if you intend to use it, you should probably know how you intend to use(call) it.

Using typeof is useful if a variable’s existence is not known, and checking its value if its existence is known.

So the use depends on the situation, and knowing what each one does and when they are useful is better than knowing which one is better.

Advertisements

2 Responses to “JavaScript: typeof “undefined” vs undefined”

  1. nice tutorial! Thanks

  2. Explained well on how to look at “undefined” and undefined from different perspective.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: