O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

null and undefined

Earlier we learned that, the Null and void datatypes each include a single value only—null and undefined, respectively. Now that we have studied datatypes and type annotations, let's consider the difference between those two values.

Both null and undefined conceptually represent the absence of data. The null value represents the absence of data for variables, parameters, and return values with a specified type annotation set to anything but Boolean, int, uint, and Number. For example, the following code creates a typed instance variable, pet, of type VirtualPet. Before the variable is explicitly assigned a value in the program, its value is null.

package {
  import flash.display.Sprite;
  import zoo.*;

  public class VirtualZoo extends Sprite {
    private var pet:VirtualPet;

    public function VirtualZoo () {
      trace(pet);  // Displays: null
    }
  }
}

By contrast, the undefined value represents the absence of data for variables, parameters, and return values without a specified type annotation. For example, the following code creates an object with two dynamic instance variables, city and country. When assigning the country variable an initial value, the code uses undefined to indicate that country does not yet have a meaningful value.

var info = new Object();
info.city = "Toronto";
info.country = undefined;

The undefined value also represents the complete absence of a variable or method on an object whose class is defined as dynamic. For example, the following attempt to access a nonexistent ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required