User Guide
Operators 133
Usage 3: Variables associated with dynamic and input text fields have the data type String. In
the following example, the variable
deposit is an input text field on the Stage. After a user
enters a deposit amount, the script attempts to add
deposit to oldBalance. However,
because
deposit is a String data type, the script concatenates (combines to form one string)
the variable values rather than summing them.
var oldBalance:Number = 1345.23;
var currentBalance = deposit_txt.text + oldBalance;
trace(currentBalance);
For example, if a user enters 475 in the deposit text field, the trace() statement sends the value
4751345.23 to the Output panel. To correct this, use the Number() function to convert the
string to a number, as in the following:
var oldBalance:Number = 1345.23;
var currentBalance:Number = Number(deposit_txt.text) + oldBalance;
trace(currentBalance);
The following example shows how numeric sums to the right of a string expression are not
calculated:
var a:String = 3 + 10 + "asdf";
trace(a); // 13asdf
var b:String = "asdf" + 3 + 10;
trace(b); // asdf310
+= addition assignment operator
expression1 += expression2
Assigns expression1 the value of expression1+ expression2. For example, the following
two statements have the same result:
x += y;
x = x + y;
This operator also performs string concatenation. All the rules of the addition (+) operator
apply to the addition assignment
(+=) operator.
Availability: ActionScript 1.0; Flash Player 4
Operands
expression1 : Number - A number or string.
expression2 : Number - A number or string.
Returns
Number - The result of the addition.