JavaScript Roadmap — Day 7: Arithmetic Operators
Arithmetic Operators
The building blocks of every calculation in JavaScript — and the hidden behaviors that trip up every beginner.
Look at this code. A developer is building a shopping cart. Can you spot the bug before reading on?
The answer:
JavaScript calculated tax * discount first (50 × 2 = 100), then added price (500 + 100 = 600). The developer forgot that multiplication runs before addition. The fix is simple: (price + tax) * discount. This is operator precedence — and it is the source of real bugs in real projects every single day.
In this lesson you will learn all 7 arithmetic operators deeply — including the hidden behaviors that most tutorials completely skip. By the end you will never write a wrong calculation again.
1. The 7 Arithmetic Operators
JavaScript gives you exactly 7 arithmetic operators. Five of them you already know from school. Two of them — % and ** — are unique to programming and deserve their own deep sections below.
| Operator | Name | Example | Result | Real use |
|---|---|---|---|---|
+ | Addition | 5 + 3 | 8 | Totals, scores |
- | Subtraction | 5 - 3 | 2 | Discounts, balance |
* | Multiplication | 5 * 3 | 15 | Area, quantity × price |
/ | Division | 10 / 3 | 3.333 | Averages, ratios |
% | Remainder | 10 % 3 | 1 | Even/odd, cycles |
** | Exponentiation | 2 ** 8 | 256 | Powers, growth |
-x | Unary negation | -5 | -5 | Flip sign |
2. Operator Precedence — Who Goes First?
When you write an expression with multiple operators JavaScript has rules for which one runs first. This is called operator precedence — and it is exactly the same as the BODMAS/PEMDAS rules you learned in school. Most developers forget it exists until it causes a bug.
The order from highest to lowest priority:
| Priority | Operator | Runs |
|---|---|---|
| 1st — Highest | ( ) | Brackets always first |
| 2nd | ** | Exponentiation |
| 3rd | * / % | Multiply, divide, remainder |
| 4th — Lowest | + - | Addition and subtraction |
🔥 Professional habit: When in doubt — always use brackets. (price + tax) * quantity is clearer than relying on precedence rules. Brackets cost nothing and prevent every precedence bug forever.
3. Increment ++ and Decrement --
++ adds 1 to a variable. -- subtracts 1. Simple. But where you put them — before or after the variable — changes what value is returned. This is one of the most common JavaScript interview questions and a real source of bugs.
💡 Simple rule: In a plain loop like i++ — prefix and postfix make no difference. The difference only matters when you use the value in the same expression. When in doubt — do the increment on its own line and keep the code readable.
4. The % Remainder Operator — More Powerful Than You Think
The % operator gives you the leftover after dividing two numbers. It looks simple but it solves problems that would otherwise take 5 lines of code to write. Every JavaScript developer uses it constantly.
% is a remainder operator — the result takes the sign of the first number. -7 % 3 gives -1 in JavaScript but 2 in pure maths. To get true mathematical modulo use: ((n % m) + m) % m.%. To know which page an item belongs to: Math.floor(itemIndex / itemsPerPage). To know position within a page: itemIndex % itemsPerPage. These two lines power every list, grid, and table in every web app.5. The ** Exponentiation Operator — The Brain Bender 🔥
** raises a number to a power. 2 ** 8 means 2 to the power of 8 = 256. It was added in ES2017 as a cleaner alternative to Math.pow(). But it has one behavior that catches everyone off guard — it reads right to left instead of left to right.
🔥 The right-to-left rule: ** is the only JavaScript operator that evaluates right to left. Every other operator goes left to right. This makes 2 ** 3 ** 2 = 512 not 64. Always use brackets with chained ** to be explicit about your intent.
6. The Math Object — Your Extra Calculator
JavaScript has a built-in Math object with mathematical functions that go beyond basic arithmetic. These are used in almost every real project — from rounding prices to generating random numbers to calculating geometry.
Math.floor(-4.5) gives -5 because floor always rounds toward negative infinity. Math.trunc(-4.5) gives -4 because trunc just removes the decimal. For positive numbers they give the same result. For negative numbers they are different. Always know which one you need.Math.random() returns a number from 0 (inclusive) to 1 (exclusive). It can give 0 but never gives exactly 1. This is why the randInt formula uses max - min + 1 — to include the maximum value in the possible results.10 / 3 gives 3.333... not 3. Use Math.floor(10 / 3) to get 3. There is also a bitwise trick (10 / 3) | 0 which is faster but only works for numbers under 2 billion — use Math.floor() for safety.7. Try It Yourself
Edit the code and click Run Code. Try changing the numbers and operators. Pay special attention to the results that surprised you earlier.
8. Practice Tasks
Task 1 — Easy: Circle Area Calculator
Calculate the area of a circle with radius 7 using the formula πr². Use Math.PI and the ** operator. Round the result to 2 decimal places using .toFixed(2). Log the result as: "Area: 153.94"
Task 2 — Medium: Temperature Converter
Write a function celsiusToFahrenheit(c) that converts Celsius to Fahrenheit using the formula: F = (C × 9/5) + 32. Test with 0°C (should give 32°F), 100°C (should give 212°F), and 37°C (body temperature — should give 98.6°F). Watch operator precedence carefully!
Task 3 — Hard: Shopping Cart with Correct Precedence
Fix the bug from the beginning of this lesson. Write a function calculateTotal(price, tax, quantity) that correctly calculates: (price + tax) × quantity. Then add a discount: if quantity is 5 or more apply 10% off using % to check if quantity is divisible by 5. Use proper brackets everywhere. Test with price=500, tax=50, quantity=5.