User Guide
54 ActionScript Language and Syntax
An interesting implication of the lack of block-level scope is that you can read or write to a
variable before it is declared, as long as it is declared before the function ends. This is because
of a technique called hoisting, which means that the compiler moves all variable declarations
to the top of the function. For example, the following code compiles even though the initial
trace() function for the num variable happens before the num variable is declared:
trace(num); // NaN
var num:Number = 10;
trace(num); // 10
The compiler will not, however, hoist any assignment statements. This explains why the
initial
trace() of num results in NaN (not a number), which is the default value for variables
of the Number data type. This means that you can assign values to variables even before they
are declared, as shown in the following example:
num = 5;
trace(num); // 5
var num:Number = 10;
trace(num); // 10
Default values
A default value is the value that a variable contains before you set its value. You initialize a
variable when you set its value for the first time. If you declare a variable, but do not set its
value, that variable is uninitialized. The value of an uninitialized variable depends on its data
type. The following table describes the default values of variables, organized by data type:
For variables of type Number, the default value is
NaN (not a number), which is a special value
defined by the IEEE-754 standard to mean a value that does not represent a number.
Data type Default value
Boolean
false
int 0
Number
NaN
Object
null
String
null
uint 0
Not declared (equivalent to type annotation
*)
undefined
All other classes, including user-defined classes.
null