Undefined? Didn't you mean undefined?

After publishing my latest post, I’ve received a couple of emails from fellow readers asking me if I didn’t mess up things when I’ve mentioned Undefined. Shouldn’t it be undefined? And the answer is no, I didn’t make an error because I was talking about types (and not about values). This kind of detail is important if you intend to become a proficient JavaScript developer (which means reading the spec). So, if you read the spec, you’ll see that it talks about the Undefined type and about the undefined value.

Undefined is a type whose sole value is undefined (notice the lower case u). The undefined (lower case u again!) value is a primitive value (recall that Undefined is one of the primitive types) that is used when a variable hasn’t been assigned a value. Here’s an example:

var aux;
//aux === undefined is true

To sum up, whenever you declare a variable and don’t initialize it, it will end up with an undefined value. Notice that the existence of undefined value lets you distinguish between declared variables that have been initialized or not. Take a look at the following example:

var aux;
//aux ===  undefined is true
var aux2 = null;
//aux2 === undefined is false

As you can see, you can initialize a variable to null. When this happens, the value no longer returns undefined, but null. null is also a primitive value and it was introduced in the language to represent the intentional absence of any value (notice that a variable initialized or set to null has been initialized and that means it’s not undefined). Since we’re talking about null, I’m going to take this opportunity to mention that null is the only value of the primitive Null type (yes, we have those nasty case differences between type and value again Smile).

And I guess that it’s all for now. Stay tuned for more!


~ by Luis Abreu on October 28, 2012.

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: