Arithmetic operators perform mathematical calculations on numeric operands. JavaScript supports standard arithmetic along with modulo (%), exponentiation (**), unary negation, and increment/decrement operators.
flowchart LR
Arith["Arithmetic Operators"] --> Basic["Basic (+, -, *, /)"]
Arith --> Power["Exponentiation (**)"]
Arith --> Remainder["Modulo / Remainder (%)"]
Arith --> IncDec["Increment ++ / Decrement --"]
| Operator | Name | Example | Explanation |
|---|---|---|---|
+ |
Addition | 10 + 5 |
Calculates sum (or string concatenation if string operand present). |
- |
Subtraction | 10 - 5 |
Calculates difference. |
* |
Multiplication | 10 * 5 |
Calculates product. |
/ |
Division | 10 / 4 |
Calculates quotient (2.5). |
% |
Modulo | 10 % 3 |
Returns remainder of integer division (1). |
** |
Exponentiation | 2 ** 3 |
Calculates base raised to power (8). |
++ / -- |
Inc / Dec | x++ vs ++x |
Post-increment returns value before incrementing; Pre-increment updates first. |
// Demonstrating arithmetic operators and pre vs post increment
// 1. Basic Arithmetic & Modulo
let a = 15;
let b = 4;
console.log(`Sum: ${a + b}`); // 19
console.log(`Quotient: ${a / b}`); // 3.75
console.log(`Remainder (15 % 4): ${a % b}`); // 3
console.log(`Exponentiation (2 ** 10): ${2 ** 10}`); // 1024
// 2. Pre-increment vs Post-increment
let counter1 = 5;
let result1 = counter1++; // Post-increment: assigns 5 to result1, then counter1 becomes 6
console.log(`Post-increment: result1=${result1}, counter1=${counter1}`);
let counter2 = 5;
let result2 = ++counter2; // Pre-increment: counter2 becomes 6, then assigns 6 to result2
console.log(`Pre-increment: result2=${result2}, counter2=${counter2}`);
// 3. Addition Coercion Pitfall
console.log(`Number + Number: ${10 + 20}`); // 30
console.log(`Number + String: ${10 + "20"}`); // "1020" (Concatenation!)
console.log(`String - Number: ${"30" - 10}`); // 20 (Coerced to number!)
+ expression is a string, JavaScript converts the other operand to a string and concatenates them.Math.floor(a / b) for Integer Division: JavaScript division / returns floating-point numbers.% 2 === 0 to Check Even Numbers: Modulo % is the standard pattern for checking even/odd parity or array wrap-around indexing.What are the final values of x and y after executing let x = 3; let y = x++ + ++x;?
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.