Arithmetic Operators
Now that you know how to store data in variables, you need to know how to manipulate it.
Arithmetic operators are symbols that perform math operations on numeric values.
const total = 10 + 5;
console.log(total); // 15You will use arithmetic operators for totals, counters, indexes, prices, scores, measurements, timers, and many other calculations.
Core Arithmetic Operators
JavaScript supports the standard arithmetic operators you know from math.
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
+ |
Addition | Adds numbers or concatenates strings | 10 + 5 |
15 |
- |
Subtraction | Subtracts the right value from the left value | 10 - 5 |
5 |
* |
Multiplication | Multiplies values | 10 * 5 |
50 |
/ |
Division | Divides the left value by the right value | 10 / 2 |
5 |
% |
Remainder | Returns the remainder after division | 10 % 3 |
1 |
** |
Exponentiation | Raises a value to a power | 2 ** 3 |
8 |
Addition
Use + to add numbers.
const total = 10 + 5;
console.log(total); // 15The + operator also joins strings.
console.log("Hello" + " " + "World"); // Hello WorldBe careful when mixing strings and numbers:
console.log("5" + 1); // "51"Because one value is a string, JavaScript performs string concatenation.
Subtraction
Use - to subtract numbers.
const remaining = 10 - 3;
console.log(remaining); // 7If a numeric string is used with subtraction, JavaScript may coerce it into a number:
console.log("10" - 3); // 7Even though this works, explicit conversion is usually clearer:
const value = Number("10") - 3;
console.log(value); // 7Multiplication
Use * to multiply numbers.
const price = 100;
const quantity = 3;
const total = price * quantity;
console.log(total); // 300Division
Use / to divide numbers.
console.log(10 / 2); // 5
console.log(10 / 3); // 3.3333333333333335Division can produce decimal values.
Dividing by zero gives Infinity or -Infinity.
console.log(1 / 0); // Infinity
console.log(-1 / 0); // -InfinityRemainder Operator
The % operator returns the remainder after division.
console.log(10 % 3); // 1
console.log(10 % 2); // 0
console.log(7 % 4); // 3Think of 10 % 3 like this:
3fits into10three times.3 * 3is9.1is left over.
So the result is 1.
Checking Even or Odd Numbers
The remainder operator is commonly used to check whether a number is even or odd.
const number = 10;
console.log(number % 2 === 0); // trueIf a number divided by 2 has a remainder of 0, it is even.
const number = 7;
if (number % 2 === 0) {
console.log("Even");
} else {
console.log("Odd");
}Exponentiation
Use ** to raise a number to a power.
console.log(2 ** 3); // 8
console.log(5 ** 2); // 252 ** 3 means:
2 * 2 * 2Increment and Decrement
The increment operator ++ adds 1 to a variable.
The decrement operator -- subtracts 1 from a variable.
let count = 0;
count++;
console.log(count); // 1
count--;
console.log(count); // 0These operators only work on variables or writable properties, not fixed values.
// 5++; // ErrorPostfix Increment
Postfix means the operator comes after the variable.
let count = 5;
console.log(count++); // 5
console.log(count); // 6With postfix:
- JavaScript returns the current value.
- Then it updates the variable.
Prefix Increment
Prefix means the operator comes before the variable.
let count = 5;
console.log(++count); // 6
console.log(count); // 6With prefix:
- JavaScript updates the variable.
- Then it returns the new value.
The same idea applies to decrement:
let score = 10;
console.log(score--); // 10
console.log(score); // 9
console.log(--score); // 8
console.log(score); // 8Prefer Clear Updates
Prefix and postfix behavior can be confusing.
In many situations, this is clearer:
count = count + 1;Or:
count += 1;Use ++ and -- when they improve readability, such as simple counters. Avoid using them inside complicated expressions.
Shorthand Assignment Operators
When updating a variable based on its current value, you can combine arithmetic with assignment.
let x = 10;
x += 5; // x = x + 5
x -= 3; // x = x - 3
x *= 2; // x = x * 2
x /= 4; // x = x / 4
x %= 4; // x = x % 4Step by step:
let x = 10;
x += 5;
console.log(x); // 15
x -= 3;
console.log(x); // 12
x *= 2;
console.log(x); // 24
x /= 4;
console.log(x); // 6
x %= 4;
console.log(x); // 2These are useful for counters, totals, and repeated calculations.
Operator Precedence
JavaScript follows an order of operations.
In general:
- Parentheses
() - Exponentiation
** - Multiplication
*, division/, remainder% - Addition
+, subtraction-
Example:
console.log(2 + 3 * 4); // 14Multiplication happens first:
3 * 4 = 12
2 + 12 = 14Use parentheses to make the order explicit:
console.log((2 + 3) * 4); // 20Parentheses make your intention clear to both JavaScript and other developers.
A Practical Example
Imagine a shopping cart calculation:
const itemPrice = 499;
const quantity = 3;
const discount = 100;
const subtotal = itemPrice * quantity;
const finalTotal = subtotal - discount;
console.log(subtotal); // 1497
console.log(finalTotal); // 1397Now add tax:
const taxRate = 0.18;
const tax = finalTotal * taxRate;
const totalWithTax = finalTotal + tax;
console.log(totalWithTax);Arithmetic operators are the foundation of calculations like this.
Common Mistakes
Mixing Strings and Numbers with +
console.log("10" + 5); // "105"If you want math, convert first:
console.log(Number("10") + 5); // 15Expecting % to Return a Percentage
The % operator does not calculate percentages. It returns a remainder.
console.log(10 % 3); // 1To calculate 20% of 100:
console.log(100 * 0.2); // 20Hiding Logic in Increment Expressions
This is harder to read:
let count = 1;
const result = count++ + ++count;Prefer simple steps:
let count = 1;
count += 1;
count += 1;Summary
Arithmetic operators let you perform calculations in JavaScript.
Remember:
- Use
+,-,*, and/for basic math. - Use
%to get the remainder after division. - Use
% 2to check even and odd numbers. - Use
**for exponentiation. ++and--update values by1, but prefix and postfix behave differently.- Shorthand assignment operators like
+=and*=update a variable based on its current value. - JavaScript follows operator precedence, but parentheses make code clearer.
- Be careful when using
+with strings and numbers together.
Next, you will learn about assignment operators in more detail.