Wednesday, February 13, 2013

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.
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

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.If you declare a variable, but do not declare its data type, the default data type * will apply, which actually means that the value is untyped. If you also do not initialize an untyped variable with a value, then its default value is undefined.

For data types other than Boolean, Number, int and uint, the default value of any uninitialized variable is null. This applies to all of the classes defined by the Flash Player API, as well as any custom classes that you create.

The value null is not a valid value for variables of type Boolean, Number, int or uint. If you attempt to assign a value of null to a such a variable, the value is converted to the default value for that data type. For variables of type Object, you can assign a value of null. If you attempt to assign the value undefined to a variable of type Object, the value is converted to null.

For variables of type Number, there is a special top-level function called isNaN() that returns the Boolean value true if the variable is not a number, and false otherwise.
Programming Actionscript 3. Powered by Blogger.