Variables and String Conversion
Top  Previous  Next


Variables can be used to store numbers and strings.

The exact item stored by the variable is converted, as appropriate, depending on the context in which the variable is used.

The operators +, <, <=, ==, !=, => and > can take numbers, strings or variables as their arguments.

In SWF4 export only, variables are assumed to be numeric and conversion is done as appropriate. In SWF5 or later, the type of the actual value stored in the variable is used.

The + operator has String Constant Precedence.
The <, <=, ==, !=, => and > operators have Numeric Constant Precedence.

The operators add, lt, le, eq, ne, ge, gt assume that variables are strings and have String Constant Precedence.

Consider the following trace statements:

a = 11;
b = "22";
trace(a + "987");      // string parameter causes conversion of 11 to "11" result is 11987
trace(a ADD 987);   // ADD operator converts both a and 987 to strings, result is 11987
trace(a + 987);      // both parameters are numbers, result is 998
trace("987" + b);      // both parameters are strings, result is 98722
trace(987 ADD b);   // ADD operator converts 987 to string, result is 98722
trace(987 + b);      // variable is assumed to be number. "22" converted to number, result is 1009
trace(a + b);      // variables assumed to be numbers. "22" converted to 22, result is 33


During conversion from string to number, 0 is returned if the string does not represent a number.

onLoad () {
five = 5;
six = 06;
fivestr = "5";
sixstr = "06";
if ("5" < "6") {
trace("test1");
}
if ("5" < "06") {
trace("test2");
}
if (5 < 06) {
trace("test3");
}
if (5 lt 06) {
trace("test4");
}
if (fivestr < sixstr) {
trace("test5");
}
if (fivestr lt sixstr) {
trace("test6");
}
if (five < six) {
trace("test7");
}
if (five lt six) {
trace("test8");
}
}


returns
test1
test3
test4
test5
test7
test8