Understanding the ActionScript Language > About variables > Typing a variable |
![]() ![]() ![]() |
Typing a variable
In Flash, you do not need to explicitly define a variable as holding either a number, a string, or other data type. Flash determines the data type of a variable when the variable is assigned:
x = 3;
In the expression x = 3
, Flash evaluates the element on the right side of the operator and determines that it is of type number. A later assignment may change the type of x
; for example, x = "hello"
changes the type of x
to a string. A variable that hasn't been assigned a value has a type of undefined
.
ActionScript converts data types automatically when an expression requires it. For example, when you pass a value to the trace
action, trace
automatically converts the value to a string and sends it to the Output window. In expressions with operators, ActionScript converts data types as needed; for example, when used with a string, the +
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"
When you debug scripts, it's often useful to determine the data type of an expression or variable to understand why it is behaving a certain way. You can do this with the typeof
operator, as in this example:
trace(typeof(variableName
));
To convert a string to a numerical value, use the Number
function. To convert a numerical value to a string, use the String
function. For detailed information, see Number (function) and String (function) in the ActionScript Dictionary.
![]() ![]() ![]() |