User Guide
40 Chapter 2: ActionScript Basics
• Values returned from a function or method
• Objects created as subclasses of existing classes
In ActionScript 2.0, you should explicitly assign data types to items, which can help prevent or
diagnose certain errors in your scripts at compile time and offers other benefits. This technique is
called strict data typing.
For more information on assigning data types, see the following topics:
• “Automatic data typing” on page 40
• “Strict data typing” on page 41
• “Casting objects” on page 42
• “Determining an item’s data type” on page 43
Automatic data typing
If you do not explicitly define an item as holding either a number, a string, or another data type,
Flash Player will, at runtime, try to determine the data type of an item when it is assigned. If you
assign a value to a variable, as shown in the following example, Flash Player evaluates at runtime
the element on the right side of the operator and determines that it is of the Number data type:
var x = 3;
A later assignment might change the type of x; for example, the statement x = "hello" changes
the type of
x to a string. Because x was not declared using strict data typing, the compiler cannot
determine the type; to the compiler, the variable
x can have a value of any type. See “Strict data
typing” on page 41.
ActionScript converts data types automatically when an expression requires it and the variables
aren’t strictly typed. For example, when you pass a value to the
trace() statement, trace()
automatically converts the value to a string and sends it to the Output panel.
In expressions with operators, ActionScript converts data types as needed; in the following
example, when used with a string, the addition (
+) operator expects the other operand to be
a string:
"Next in line, number " + 7
ActionScript converts the number 7 to the string "7" and adds it to the end of the first string,
resulting in the following string:
"Next in line, number 7"
Strict data typing is recommended; for more information, see “Strict data typing” on page 41.