Arithmetic Operators
These operators are used to perform arithmetic between variables and/or values. In this table, the y-variable has a value of 5 to explain the JavaScript Arithmetic operators:
Operator | Description | Example | Result of x | Result of y |
---|---|---|---|---|
+ | Addition | x=y+2 (5+2) | 7 | 5 |
- | Subtraction | x=y-2 (5-2) | 3 | 5 |
* | Multiplication | x=y*2 (5*2) | 10 | 5 |
/ | Division | x=y/2 (5/2) | 2.5 | 5 |
% | Modulus (division remainder) | x=y%2 (remainder is 1 for the equation 5/2) | 1 | 5 |
++ | Increment | x=++y (++6) | 6 (a) | 6 (b) |
x=y++ (6++) | 5 (c) | 6 (b) | ||
-- | Decrement | x=--y (--4) | 4 (d) | 4 (e) |
x=y-- (4--) | 5 (f) | 4 (e) | ||
(a) The increment occurs before the variable, so x is the incremented value of y, which is 6; (b) The value is 6 because y, which is 5, is increased by 1 for this operator; (c) The increment occurs after the variable, so x is the original value of y, which is 5; (d) The decrement occurs before the variable, so x is the decremented value of y, which is 4; (e) The value is 4 because y, which is 5, is decreased by 1 for this operator; (f) The decrement occurs after the variable, so x is the original value of y, which is 5. |