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. 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 (""), and true otherwise.
var str1:String; // uninitialized string is null
trace(Boolean(str1)); // output: false
var str2:String = ""; // empty string
trace(Boolean(str2)); // output: false
var str3:String = " "; // white space only
trace(Boolean(str3)); // output: true
Casting to Boolean from an instance of the Object class returns false if the instance is null, and true otherwise.
var myObj:Object; // uninitialized object is null
trace(Boolean(myObj)); // output: false
myObj = new Object(); // instantiate
trace(Boolean(myObj)); // output: true
Boolean variables get special treatment in strict mode in that you can assign values of any data type to a Boolean variable without casting. Implicit coercion from all data types to the Boolean data type occurs even in strict mode. In other words, unlike almost all other data types, casting to Boolean is not necessary to avoid strict mode errors. The following examples all compile in strict mode and behave as expected at runtime.
var myObj:Object = new Object(); // instantiate
var bool:Boolean = myObj;
trace(bool); // Output: true
bool = "random string";
trace(bool); // Output: true
bool = new Array();
trace(bool); // Output: true
bool = NaN;
trace(bool); // Output: false