← Back to JavaScript Course | Chapter 2: Functions & Objects | Lesson 1 of 10

JS Arithmetic

Every calculator you've ever used, from a simple pocket one to the calculator app on your phone, understands the same basic operations, add, subtract, multiply, divide. JavaScript's arithmetic operators give your code that exact same calculator power, plus a few extras like exponents and remainders, letting scripts compute totals, percentages, and countdowns on the fly. Whether it's calculating a reading time estimate or a quiz score on cookiescursor.com, arithmetic operators are doing the math behind the scenes.

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

javascript
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

javascript
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

javascript
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

javascript
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

javascript
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));
Common Mistakes
  1. Forgetting operator precedence, multiplication and division run before addition and subtraction unless parentheses say otherwise.
  2. Confusing the remainder operator % with division /, % returns what's left over, not the divided result.
  3. Mixing up post-increment and pre-increment, x++ and ++x behave differently when used inside a larger expression.
Chapter Summary
  • 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.
Browser Support

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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.