26. Arithmetic Operators
Arithmetic operators perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | 5 + 3 (8) |
- | Subtraction | 5 - 3 (2) |
* | Multiplication | 5 * 3 (15) |
/ | Division | 5 / 3 (1.666...) |
% | Modulus (Remainder) | 5 % 3 (2) |
** | Exponentiation (Power) | 5 ** 2 (25) |
The Modulus Operator (%)
This is useful for determining if a number is even or odd, or for cycling through lists.
javascript let x = 10; console.log(x % 2); // Output: 0 (10 is even)
let y = 11; console.log(y % 2); // Output: 1 (11 is odd)
Order of Operations (Precedence)
JS follows standard mathematical rules (PEMDAS/BODMAS):
- Parentheses
() - Exponents
** - Multiplication
*, Division/, Modulus%(left-to-right) - Addition
+, Subtraction-(left-to-right)
javascript let result = 10 + 2 * 5; // 10 + 10 = 20 let trickyResult = (10 + 2) * 5; // 12 * 5 = 60