OPERATORS: + ++ -
-- * / %
JavaScript contains the standard arithmetic operators plus ones
for modulus, increment, decrement and unary negation.
+
This is the standard addition operator and returns the sum of
two numerical values (either literals or variables):
Code:
x = a + 6;
++
This is the increment operator and is used to increment (add one to)
its operand. The position of the operator in relation to its operand
determines whether the increment takes place before or after returning
a value. If it is placed after the operand, then it returns the value
before incrementing as in the following example which, assuming 'a'
to be 5, sets 'x' to 5 and then increments 'a' to 6:
Code:
x = a++;
...whereas, if the operator is placed before the operand, 'a' of
the above example will first be incremented to 6,
and then the value 6 will be assigned to 'x':
Code:
x = ++a;
-
This is the standard subtraction operator and it subtracts
one number from another:
Code:
x = a - 6;
This is also the unary negation operator which precedes and
negates its operand. In the following example with 'a' being 6,
the value -6 is assigned to the variable 'x' while 'a'
retains the value 6:
Code:
x = -a;
--
This is the decrement operator which decrements (deducts one from)
its operand. As with the increment operator, the position of the
decrement operator in relation to its operand determines whether
the decrement takes place before or after the assignment operation.
If it is placed after the operand, then a value is returned before
decrementing, as in the next example which, assuming 'a' to be 6,
assigns 6 to the variable 'x' and then decrements 'a' to 5:
Code:
x = a--;
...while, if the decrement operator is placed before its operand,
the decrement takes place before the assignment. Assuming 'a'
to be 6, this next example decrements 'a' to 5 and then sets
the variable 'x' to 5:
Code:
x = --a;
*
This is the standard multiplication operator and it returns
the product of two numerical values (either literals or variables):
Code:
x = a * 7;
/
This is the standard division operator which divides one number by another:
Code:
x = a / 7;
%
This is the modulus operator which returns the integer remainder of dividing
the preceding operand by the one following it. The next example returns the
value 2 to the variable 'x':
Code:
x = 9 % 7;
Copyright 1999 by Infinite Software Solutions, Inc.
Trademark Information