Loading lessons...
JavaScript Arithmetic
JavaScript Arithmetic
Arithmetic operators perform math on numbers.
The basic operators
| Operator | Description | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 2 | 7 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 5 * 2 | 10 |
/ | Division | 5 / 2 | 2.5 |
% | Modulus (remainder) | 5 % 2 | 1 |
** | Exponentiation | 5 ** 2 | 25 |
Increment and decrement
let x = 5;
x++; // x becomes 6
x--; // x becomes 5
Precedence
Like in math class, JavaScript follows order of operations. Multiplication and division happen before addition and subtraction:
let result = 2 + 3 * 4; // 14, not 20
Use parentheses to control the order:
let result = (2 + 3) * 4; // 20
The + on strings
The + operator also joins strings:
"Hello" + " " + "World" // "Hello World"
TL;DR
- Arithmetic: + - / % *.
++adds one,--subtracts one.- Multiplication and division come before addition.
- Parentheses control the order.
+joins strings too.