
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Arithmetic Operators
|
117
and “5” are converted to the numbers 3 and 5 before the multiplication is per-
formed. 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.
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 “Join-
ing Strings Together” 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 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 ...