Arithmetic operators are used to perform arithmetic on numbers (literals or variables).
Operator | Description | Example |
---|---|---|
+ | Addition | The addition operator adds numbers:
var x = 5, y = 2, z = x + y; (The value of z is 7) |
- | Subtraction | The subtraction operator subtracts numbers:
var x = 5, y = 2, z = x - y; (The value of z is 3) |
* | Multiplication | The multiplication operator multiplies numbers:
var x = 5, y = 2, z = x * y; (The value of z is 10) |
/ | Division | The division operator divides numbers:
var x = 5, y = 2, z = x / y; (The value of z is 2.5) |
% | Modulus | The modulus operator computes the remainder from the division of the numbers:
var x = 5, y = 2, z = x % y; (5 ÷ 2 = 2 with remainder of 1 leftover, so the value of z is 1) |
++ | Increment | The increment operator adds 1 to the number:
var x = 5, z = ++x; (The value of z is 6) |
-- | Decrement | The decrement operator subtracts 1 from the number:
var x = 5, z = --x; (The value of z is 4) |