Table of Contents

5.3 Arithmetic Operators

The arithmetic operators perform mathematical operations on numeric operands. If you use nonnumeric operands with most of the arithmetic operators, ActionScript attempts to convert the foreign data to a number. For example, false - true evaluates to -1, because false converts to the numeric value 0 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 string concatenation, not mathematical addition.

If an attempt to convert a nonnumeric operand to a number fails, the operand is 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.

5.3.1 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.

5.3.2 Increment

A handy variation on addition, the increment operator accepts a single operand and simply adds 1 to its current value. Increment has two general forms, called prefix increment and postfix increment, as follows:

++operand    // Prefix increment
operand++    // Postfix increment

In both forms, increment adds 1 to a variable, array element, or object property, such as:

var x = 1;
x = x + 1;  // x is now 2 (the verbose syntax)
++x;        // Add 1 using the prefix operator: x is now 3
x++;        // Add 1 using the postfix operator: x is now 4

When used in isolation, there is no difference between prefix and postfix increment, although postfix increment is more common by convention.

However, when used in larger expressions, prefix and postfix increment have different behaviors: prefix increment adds 1 to operand and returns the value of operand + 1; postfix increment adds 1 to operand but returns the value of operand itself, not operand + 1:

var x = 1;
// Prefix increment: x is incremented first, so y is set to 2
var y = ++x;
   
var x = 1;
// Postfix increment: y is set to 1, then x is incremented to 2
var y = x++;

We'll revisit the increment operators in Chapter 8.

5.3.3 Subtraction

The subtraction operator subtracts the second operand from the first operand. It takes the general form:

operand1 - operand2

The operands can be any valid expression. If either operand is not of the number type, and conversion to a number fails, the operation yields NaN:

234 - 5  // Yields 229
5 - 234  // Yields -229

To determine the absolute (i.e., positive) difference between two numbers, see the Math.abs( ) method in the ActionScript Language Reference.

5.3.4 Decrement

The decrement operator is analogous to the increment operator, but it subtracts 1 from its operand's current value instead of adding 1. Like increment, decrement has two general forms, called prefix decrement and postfix decrement, as follows:

--operand  // Prefix decrement
operand--  // Postfix decrement

In both forms, it is used to subtract 1 from a variable, array element, or object property. Prefix decrement subtracts 1 from operand and returns the value of operand - 1; postfix decrement subtracts 1 from operand but returns the value of operand itself, not operand - 1. As with the increment operators, the form of decrement used matters only if the operand is part of a larger expression:

var x = 10;
var y;
x = x - 1;    // x is now 9
--x;          // x is now 8 (prefix decrement)
x--;          // x is now 7 (postfix decrement)
y = --x;      // y is now 6, x is now 6 (prefix decrement)
y = x--;      // y is still 6, x is now 5 (postfix decrement)

5.3.5 Multiplication

The multiplication operator multiplies two numeric operands and returns the result (i.e., the product). Multiplication takes the general form:

operand1 * operand2

The operands can be any valid expression. The * symbol used for multiplication is in lieu of the x ("times") symbol used in traditional mathematics. If either operand is not of the number type, and conversion to a number fails, the operation yields NaN:

6 * 5  // Returns 30

5.3.6 Division

The division operator divides the first operand (the numerator) by the second operand (the divisor) and returns the result (the quotient). Division takes the general form:

operand1 / operand2

The operands must be valid numeric expressions. The / symbol used for division is in lieu of the ÷ symbol used in traditional mathematics. If either operand is not of the number type, and conversion to a number fails, the operation yields NaN. If it is necessary to express a fractional result, the quotient is a floating-point number, even if both operands are integers:

20 / 5  // Returns 4
5 / 4   // Returns 1.25; In other languages, the result may be 1, not 1.25

Note that some other languages, such as Director's Lingo language, return an integer unless at least one operand is a float.

If the divisor (denominator) is zero:

Here are some examples:

trace (0/0);   // NaN
trace ("a"/0); // NaN
trace (1/0);   // Infinity
trace (-1/0);  // -Infinity

If there is any possibility of the divisor being zero, check its value before performing the division, such as:

if (numItems != 0) {
  trace ("Average is" + total / numItems);
} else {
  trace ("There are no items for which to calculate the average");
}

Note that in some languages, attempting to divide by zero causes an error.

5.3.7 Modulo Division

The modulo operator performs so-called modulo division. It returns the remainder (i.e., modulus) that results when the first operand is divided by the second. Modulo division takes the general form:

operand1 % operand2

For example, 14 % 4 returns the value 2 because 4 divides evenly into 14 three times, with 2 being the remainder.

The operands of the modulo operator can be any valid numeric expression, including integers and (unlike in C and C++) floating-point numbers. For example, 5 % 4 is 1, and 5 % 4.5 is 0.5. If either operand is not of the number type, and conversion to a number fails, the operation yields NaN.

If a number is even, the modulo will be zero when we divide the number by two. We can use the trick shown in Example 5-1 to test whether a number is even or odd.

Example 5-1. Using modulo division to test for even numbers
var x = 3;
if (x%2 =  = 0) {
  trace("x is even");
} else {
  trace("x is odd");
}

5.3.8 Unary Negation

The unary negation operator takes only one operand. It switches the operand's sign (i.e., positive becomes negative, and negative becomes positive). Unary negation takes the general form:

-operand

The operand can be any valid expression. Here we test whether something's horizontal position is greater than the positive limit or less than the negative limit:

if (xPos > xBoundary || xPos < -xBoundary) {
  // We've gone too far
}

The unary negation operator is simply a negative sign (-). It is equivalent to the subtraction operator, where the first operand is 0, such as:

0 -operand

Table of Contents