Arithmetic Operators
The
arithmetic
operators perform mathematical operations on numeric operands. If you
use non-numeric operands with the arithmetic operators, ActionScript
will attempt to convert the foreign data to a number. For example,
false - true
evaluates to -1 because
false
converts to the numeric value
and true
converts to 1. Similarly, the expression
"3" * "5"
results in the number 15, because the
strings “3” and “5” are converted to the
numbers 3 and 5 before the multiplication is performed. The
+
operator, however, presents a special case: when
used with at least one string operand, it performs a string
concatenation operation, not mathematical addition.
If
an attempt to
convert a non-numeric operand to a number fails, the operand will be
set to the special numeric value NaN
. This results
in the entire operation yielding NaN
. Refer to
Table 3.1 for details on numeric conversion.
Addition
The addition operator returns the sum of its two operands:
operand1
+operand2
In order to return a meaningful mathematical result, the operands of
+
should be expressions that yield a numeric
value, such as:
234 + 5 // Returns 239 (2 * 3 * 4) + 5 // Returns 29
The addition operator is unique among the arithmetic operators in that if one or both of its operands are strings, it performs as a string concatenation. Refer to Section 4.6.1 in Chapter 4.
Increment
A handy variation on addition, the increment operator accepts a single operand and simply adds 1 to its current value. Increment ...
Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.