19. Null and Undefined


In This Chapter

Learn about when values don’t exist

Understand what to do with null and undefined


One of the great mysteries of the world revolves around making sense of null and undefined. Most code you see is littered with them, and you’ve probably run into them yourself a few times. As mysteries go, making sense of null and undefined isn’t particularly bizarre. It is just dreadfully boring...like the most boring (yet important) thing about JavaScript you’ll ever have to learn.

Onward!

Null

Let’s start with null. The null keyword is also sort of a primitive that fills a special role in the world of JavaScript. It is an explicit definition that stands for no value. If you’ve ever browsed through code others have written, you’ll probably see null appear quite a number of times. It is quite popular, for the advantage of null lies in its definitiveness. Instead of having variables contain stale values or mystery undefined values, setting it to null is a clear indication that you want the value to not exist.

This advantage is important when you are writing code and want to initialize or clear a variable to something that represents nothing.

Here is an example:

var test = null;

if (test === null) {
    test = "Peter Griffin";
} else {
    test = null;
}

The null primitive isn’t a naturally occurring resource. It is something you consciously assign, so you will often see it used as part of variable declarations or passed in as arguments to function calls. Using null is easy. Checking for the value of null is pretty easy as well:

if (test === null) {
    // do something interesting...or not
}

The only thing to note is that you should use the === operator instead of the lowly == one. While the world won’t end if you use ==, it’s good practice.

Undefined

Here is where things get a little interesting. To represent something that isn’t defined, you have the undefined primitive. You see undefined in a few cases, the most common ones being when you try to access a variable that hasn’t been initialized or when accessing the value of a function that doesn’t actually return anything.

Here is a code snippet that points out undefined in a few of its natural habitats:

var myVariable;
alert(myVariable); // undefined

function doNothing() {
    // watch paint dry
    return;
}

var weekendPlans = doNothing();
alert(weekendPlans); // undefined

In your code, you probably won’t be assigning undefined to anything. Instead, you will spend time checking to see if the value of something is undefined. You have several ways to perform this check. The first is a naive way that usually almost always works:

if (myVariable == undefined) {
    // do something
}

The downside of this approach is that you could potentially overwrite undefined to something like true, and that would break your code. The safest way to perform a check for undefined involves typeof and the === operator:

var myVariable;

if (typeof myVariable === "undefined") {
    alert("Define me!!!");
}

This ensures that you will perform a check for undefined and always return the correct answer.


Image Tip

Just a quick reminder for those of you reading these words in the print or e-book edition of this book: If you go to www.quepublishing.com and register this book, you can receive free access to an online Web Edition that not only contains the complete text of this book but also features a short, fun interactive quiz to test your understanding of the chapter you just read.

If you’re reading these words in the Web Edition already and want to try your hand at the quiz, then you’re in luck – all you need to do is scroll down!


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.22.164