+ addition operator

expression1 + expression2

Adds numeric expressions or concatenates (combines) strings. If one expression is a string, all other expressions are converted to strings and concatenated. If both expressions are integers, the sum is an integer; if either or both expressions are floating-point numbers, the sum is a floating-point number.

Availability: ActionScript 1.0; Flash Player 4 - In Flash 4, <code>+</code> is only a numeric operator. In Flash Player 5 and later, <code>+</code> is either a numeric operator or a string concatenator depending on the data type of the parameter. Flash 4 files that are brought into the Flash 5 or later authoring environment undergo a conversion process to maintain data type integrity. The following example illustrates the conversion of a Flash 4 file containing a numeric quality comparison: <p> Flash 4 file: <code>x + y</code> </p> <p> Converted Flash 5 or later file: <code>Number(x) + Number(y)</code> </p>

Operands

expression1 - A number or string.

expression2 : Number - A number or string.

Returns

Object - A string, integer, or floating-point number.

Example

Usage 1: The following example concatenates two strings and displays the result in the Output panel.

var name:String = "Cola"; 
var instrument:String = "Drums"; 
trace(name + " plays " + instrument); // output: Cola plays Drums 

Usage 2: This statement adds the integers 2 and 3 and displays the resulting integer, 5, in the Output panel:

trace(2 + 3); // output: 5 

This statement adds the floating-point numbers 2.5 and 3.25 and displays the resulting floating-point number, 5.75, in the Output panel

trace(2.5 + 3.25); // output: 5.75 

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