JavaScript » Arithmetic

JavaScript contains the standard arithmetic operators plus ones for modulus, increment, decrement and unary negation.


+ Operator


Examples

Code:
x = a + 6;
Explanation:

This is the standard addition operator and returns the sum of two numerical values (either literals or variables).


++ Operator


Examples

Code:
x = a++;
Explanation:

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:
...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': .

- Operator

Examples

Code:
x = a - 6;
Explanation:

This is the standard subtraction operator and it subtracts one number from another.

Code:
x = -a;
Explanation:

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

-- Operator

Examples

Code:
x = a--;
Explanation:

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

x = --a;
Explanation:

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.

* Operator

Examples

Code:
x = a * 7;
Explanation:

This is the standard multiplication operator and it returns the product of two numerical values (eith er literals or variables).

/ Operator

Examples

Code:
x = a / 7;
Explanation:

/ This is the standard division operator which divides one number by another.

% Operator

Examples

Code:
x = 9 % 7;
Explanation:

This is the modulus operator which returns the integer remainder of dividing the preceding opera nd by the one following it. The next example returns the value 2 to the variable 'x'.