O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Conversion to Primitive Types

In the preceding section we learned that when an expression is cast to a primitive type to which it does not belong, then that expression is converted to the specified type. For example, consider the following code, which casts a Date object to the primitive datatype Boolean:

Boolean(new Date())

Because Boolean is a primitive type, and the Date object does not belong to the Boolean type, ActionScript converts the Date object to the Boolean type. The result is the Boolean value true (see Table 8-5).

Cast operations are sometimes used not to tell the compiler the type of a given expression but to convert that expression to a primitive datatype.

Tip

A cast operation can convert any value to a particular primitive type.

For example, the following code converts a floating-point number (a number with a fractional value) to an integer (a number with no fractional value):

int(4.93)

The result of the preceding cast operation is the integer 4. Likewise, the following code converts the Boolean value true to the integer 1, and the Boolean value false to the integer 0:

int(true);  // Yields 1
int(false);  // Yields 0

The preceding technique might be used to reduce the size of data transmitted to a server when submitting a series of Boolean options.

Table 8-1 shows the results of converting various datatypes to the Number type.

Table 8-1. Conversion to Number

Original data

Result after conversion

undefined

NaN (the special numeric value "Not a Number," which represents invalid numeric ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required