ActionScript 101: null vs. undefined

public const undefined:*

A special value that applies to untyped variables that have not been initialized or dynamic object properties that are not initialized. In ActionScript 3.0, only variables that are untyped can hold the value undefined, which is not true in ActionScript 1.0 and ActionScript 2.0. For example, both of the following variables are undefined because they are untyped and unitialized:

  • var foo;
  • var bar:*;

The undefined value also applies to uninitialized or undefined properties of dynamic objects. For example, if an object is an instance of the Object class, the value of any dynamically added property is undefined until a value is assigned to that property.

Results vary when undefined is used with various functions:

  • The value returned by String(undefined) is "undefined" (undefined is converted to a string).
  • The value returned by Number(undefined) is NaN.
  • The value returned by int(undefined) and uint(undefined) is 0.
  • The value returned by Object(undefined) is a new Object instance.
  • When the value undefined is assigned to a typed variable, the value is converted to the default value of the data type.

null Primary expression keywords

A special value that can be assigned to variables or returned by a function if no data was provided. You can use null to represent values that are missing or that do not have a defined data type.

The value null should not be confused with the special value undefined. When null and undefined are compared with the equality (==) operator, they compare as equal. However, when null and undefined are compared with the strict equality (===) operator, they compare as not equal.

Typed variables cannot be undefined (:Object for example, but not :*).

var stateProperty:String = getStyle(“stateProperty”);
trace(stateProperty==undefined);

In ActionScript3, the code above will get the following warning: “1012: Variables of type String cannot be undefined. The value undefined will be type coerced to String before comparison.”

Here if you have time to read.

Advertisements

One Response to “ActionScript 101: null vs. undefined”

  1. Jocuri cu motociclete Says:

    THX for the information provided!

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: