JavaScript Arithmetic Operators
The standard arithmetic operators are addition (+), division (/) (returns a floating-point division in JavaScript e.g. x=1/2 returns 0.5), subtraction (-) and multiplication (*). Other arithmetic operators are:
Symbol |
Name |
Type |
Description |
Example |
|
Decrement |
Unary |
As a prefix operator ( |
If x is 2, then --x sets x to 1 and returns 1, whereas x-- sets x to 1 and returns 2. |
++ |
Increment |
Unary |
As a prefix operator (++x), it adds 1 to the value of its operand then returns it. As a postfix operator (x++), it returns the value of its operand before adding 1. |
If x is 2, then ++x sets x to 3 and returns 3, whereas x++ sets x to 3 and returns 2. |
% |
Modulus |
Binary |
Returns the integer remainder of dividing the two operands. |
11 % 5 returns 1. |
- |
Unary negation |
Unary |
Returns the negation of its operand. |
If x is 4, then -x returns -4. |
