JS Arithmetic
In this page:
The Core Operators
JavaScript's five basic arithmetic operators are + for addition, - for subtraction, * for multiplication, / for division, and % for remainder, sometimes called modulo.
Note: The % operator is especially useful for checking things like whether a number is even, since evenNumber % 2 always equals 0.
Warning: Division in JavaScript always returns a decimal when the numbers don't divide evenly, there's no automatic rounding.
Example: The Core Operators
console.log(10 + 3);
console.log(10 - 3);
console.log(10 * 3);
console.log(10 / 3);
console.log(10 % 3);
Exponentiation
The ** operator raises a number to the power of another, replacing the older, more verbose Math.pow() function for most everyday exponent calculations.
Note: ** reads naturally left to right, base ** exponent, making it intuitive once you've seen it used a couple of times.
Warning: ** binds more tightly than unary minus in some cases, so use parentheses when combining it with negative numbers to avoid ambiguity.
Example: Exponentiation
console.log(2 ** 3);
console.log(Math.pow(2, 3));
Increment and Decrement
++ increases a variable's value by one, and -- decreases it by one, each available in a pre form (++x) that changes the value before it's used, and a post form (x++) that uses the original value first.
Note: When incrementing on its own line, pre and post forms behave identically, the difference only matters when used inside a larger expression.
Warning: Mixing post-increment into a complex expression, like array[i++] = i, can produce confusing results if you're not careful about evaluation order.
Example: Increment and Decrement
let x = 5;
console.log(++x); // 6, pre-increment
let y = 5;
console.log(y++); // 5, post-increment
console.log(y); // 6
Operator Precedence
JavaScript follows standard mathematical order of operations, multiplication, division, and remainder happen before addition and subtraction, and parentheses can always be used to force a specific evaluation order.
Note: When an expression's intended order isn't immediately obvious, add parentheses even if they're not strictly required, it improves readability.
Warning: Assuming operations run strictly left to right without considering precedence rules is a common source of incorrect arithmetic results.
Example: Operator Precedence
console.log(2 + 3 * 4); // 14, multiplication first
console.log((2 + 3) * 4); // 20, parentheses force order
The Math Object
The built-in Math object provides many useful numeric functions beyond the basic operators, including Math.round(), Math.max(), Math.min(), and Math.sqrt(), covered in more detail in a later chapter.
Note: Reach for Math object methods instead of writing your own rounding or comparison logic, they're well tested and handle edge cases correctly.
Warning: Math methods like Math.max() with no arguments return -Infinity, which can cause unexpected results if a list turns out to be empty.
Example: The Math Object
console.log(Math.round(4.7));
console.log(Math.max(3, 7, 2));
console.log(Math.min(3, 7, 2));
console.log(Math.sqrt(16));
- Forgetting operator precedence, multiplication and division run before addition and subtraction unless parentheses say otherwise.
- Confusing the remainder operator % with division /, % returns what's left over, not the divided result.
- Mixing up post-increment and pre-increment, x++ and ++x behave differently when used inside a larger expression.
- JavaScript supports +, -, *, /, %, and ** for addition, subtraction, multiplication, division, remainder, and exponentiation.
- ++ and -- increase or decrease a variable by exactly one, with pre and post versions behaving differently in expressions.
- Operator precedence follows standard math rules, and the built-in Math object provides many additional numeric functions.
All arithmetic operators and the Math object are core JavaScript features supported identically in every browser, from desktop to mobile.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: