ActionScript 1.0 is dynamically typed: a variable holds any value, and its type can change with each assignment. Values convert between types on their own when an operator or function needs another type. The rules follow ECMA-262 with exceptions kept for Flash 4 movies.
The types
| Type | typeof gives | Kind | Values |
|---|---|---|---|
| String | "string" | primitive | Text. In Flash Player 5, Latin-1 or Shift-JIS; from Flash Player 6, Unicode. |
| Number | "number" | primitive | A double-precision floating-point number, including NaN and Infinity. There is no separate integer type. |
| Boolean | "boolean" | primitive | true or false. |
| Undefined | "undefined" | primitive | The single value undefined: a variable or property that has no value. |
| Null | "null" | primitive | The single value null: a deliberate empty value. |
| Object | "object" | reference | Objects of every class, including arrays, dates, XML, sounds, and from Flash MX buttons and text fields. |
| Function | "function" | reference | Functions, including constructors. |
| Movie clip | "movieclip" | reference | A movie clip instance, or a main timeline. |
Note that typeof null is "null", unlike JavaScript’s
"object", and that movie clips have a type of their own.
Primitive values are copied when assigned or passed to a function. Objects, functions
and movie clips are passed by reference: two variables can name the same object. A
primitive used with a method, as in "abc".toUpperCase(), is wrapped in a
temporary String, Number or Boolean object for the call.
Converting to a number
| Value | Becomes |
|---|---|
undefined | 0 |
null | 0 |
true, false | 1, 0 |
| A string of a decimal number | That number. Only digits, white space, a sign, a decimal point and an exponent are allowed. |
"Infinity", "-Infinity", "NaN" | Infinity, -Infinity, NaN |
Any other string, including "" | NaN |
| An array | NaN |
| A movie clip | NaN |
| Another object | The result of its valueOf() method |
The functions parseInt and parseFloat are more forgiving:
they read as much of the start of a string as makes a number, so
parseInt("12px") is 12, and parseInt takes a
radix, as in parseInt("FF", 16).
Converting to a string
| Value | Becomes |
|---|---|
undefined | "", the empty string |
null | "null" |
true, false | "true", "false" |
| A number | Its decimal form: "944.345", "0", "NaN", "Infinity" |
| An array | Its elements converted and joined with commas |
| A movie clip | Its absolute path, such as "_level0.ball" |
| Another object | The result of its toString() method, by default "[object Object]" |
Converting to a boolean
| Value | Becomes |
|---|---|
undefined, null | false |
0, NaN | false |
Any other number, including Infinity | true |
"" | false |
| Any other string | true only if it converts to a number other than 0: "1" is true, but "yes", "true" and "0" are false |
| An object, array, function or movie clip | true |
The rule for strings differs from ECMA-262, where every non-empty string is true. It
keeps Flash 4 movies working, where variables loaded from text were tested as numbers.
It catches out code like if (name), which is false for most names.
Conversion by operators
+joins strings when either side is a string, and adds otherwise:"5" + 3is"53",5 + 3is8.-,*,/,%, the bitwise operators and unary-always convert to numbers:"5" - 3is2.<,>,<=and>=compare two strings by character code, and convert to numbers otherwise.==converts when the two sides have different types:"5" == 5istrue, andnull == undefinedistrue.===, from Flash MX, never converts.!,&&,||,ifand the loops convert their tests to booleans by the table above.- The Flash 4 operators
eq,ne,lt,gt,le,geandaddalways convert both sides to strings.
When Flash 5 opens a Flash 4 document it wraps the operands of +,
==, !=, <>, <,
>, <= and >= in
Number(), unless they are number literals, because in Flash 4 those
operators were always numeric.
Converting on purpose
| To | Ways |
|---|---|
| Number | Number(x), parseInt(s, radix), parseFloat(s), x - 0 |
| String | String(x), x.toString(), n.toString(16) for another base, "" + x |
| Boolean | Boolean(x), !!x |
| Whole number | Math.floor(n), Math.round(n); the Flash 4 int(n) is deprecated |
| Test | isNaN(x), isFinite(x), typeof x, and from Flash MX x instanceof Array |
NaN is not equal to anything, itself included, so it is found only with
isNaN.
In later players
These rules hold for every SWF file that Flash 5 and Flash MX make, and Flash Player 7
and later keep them for such files. Code compiled into a SWF file for Flash Player 7 or
later follows ECMA-262 more closely: undefined becomes NaN as a
number and "undefined" as a string, and every non-empty string is true.