JS Arithmetic
In this page:
result = a + b; // also -, *, /, %, **
x++;
x--;
The Core Operators
JavaScript's five basic arithmetic operators are + for addition, - for subtraction, * for multiplication, / for division, and % for remainder, sometimes called modulo.
Example: The Core Operators
// Print `10 + 3` to the console
// Print `10 + 3` to the console
console.log(10 + 3);
// Print `10 - 3` to the console
// Print `10 - 3` to the console
console.log(10 - 3);
// Print `10 * 3` to the console
// Print `10 * 3` to the console
console.log(10 * 3);
// Print `10 / 3` to the console
// Print `10 / 3` to the console
console.log(10 / 3);
// Print `10 % 3` to the console
// Print `10 % 3` to the console
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.
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.
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.
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.
Example: The Math Object
// Print `Math.round(4.7)` to the console
// Print `Math.round(4.7)` to the console
console.log(Math.round(4.7));
// Print `Math.max(3, 7, 2)` to the console
// Print `Math.max(3, 7, 2)` to the console
console.log(Math.max(3, 7, 2));
// Print `Math.min(3, 7, 2)` to the console
// Print `Math.min(3, 7, 2)` to the console
console.log(Math.min(3, 7, 2));
// Print `Math.sqrt(16)` to the console
// Print `Math.sqrt(16)` to the console
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: