User Guide
Data types 69
Casting to Boolean
Casting to Boolean from any of the numeric data types (uint, int, and Number) results in
false if the numeric value is 0, and true otherwise. For the Number data type, the value NaN
also results in
false. The following example shows the results of casting the numbers -1, 0,
and 1:
var myNum:Number;
for (myNum = -1; myNum<2; myNum++)
{
trace("Boolean(" + myNum +") is " + Boolean(myNum));
}
The output from the example shows that of the three numbers, only 0 returns a value of
false:
Boolean(-1) is true
Boolean(0) is false
Boolean(1) is true
Casting to Boolean from a String value returns false if the string is either null or an empty
string (
""). Otherwise, it returns true.
var str1:String; // Uninitialized string is null.
trace(Boolean(str1)); // false
var str2:String = ""; // empty string
trace(Boolean(str2)); // false
var str3:String = " "; // white space only
trace(Boolean(str3)); // true
Casting to Boolean from an instance of the Object class returns false if the instance is null,
and
true otherwise, as the following example shows:
var myObj:Object; // Uninitialized object is null.
trace(Boolean(myObj)); // false
myObj = new Object(); // instantiate
trace(Boolean(myObj)); // true