Arithmetic and Assignment Operators

Updated · 7 min read
easyES2026javascriptoperatorsarithmeticassignment

An operator is a symbol that performs an operation on one or more values, called operands. In fee * 2, the operator is * and the operands are fee and 2. Arithmetic operators calculate with numbers; assignment operators store the result in a variable. The Fee Tracker uses both on every line that works out a balance, a late fee or a discount.

In One Minute

  • Arithmetic: + - * / % (remainder) ** (power).
  • ++ adds 1 and -- subtracts 1. Prefer count += 1 in most code, because it reads clearly.
  • Compound assignment: total += fee means total = total + fee. The same works for -=, *=, /=, %= and **=.
  • + joins strings: "120" + 30 is "12030". The other arithmetic operators convert strings to numbers.
  • Division never throws: 10 / 0 is Infinity, and invalid math gives NaN.

Arithmetic Operators

OperatorNameExampleResult
+Addition120 + 30150
-Subtraction120 - 3090
*Multiplication120 * 3360
/Division120 / 815
%Remainder125 % 105
**Exponentiation (power)2 ** 101024
-xNegation-feethe opposite sign
arithmetic.js▶ Try it
const termFee = 450;
const terms = 3;
 
console.log("Year:", termFee * terms);
console.log("Per month:", termFee * terms / 12);
console.log("Whole months of $100:", Math.floor(termFee / 100));
console.log("Left over:", termFee % 100);
console.log("2 to the 8th:", 2 ** 8);
Output
Year: 1350
Per month: 112.5
Whole months of $100: 4
Left over: 50
2 to the 8th: 256

Explanation: JavaScript has one number type for whole numbers and decimals, so 1350 / 12 gives 112.5 (not 112 as integer division would in Java). % gives what is left after dividing: 450 is four 100s with 50 left over.

The Remainder Operator

% is more useful than it first looks:

TaskExpression
Is n even?n % 2 === 0
Every third rowrow % 3 === 0
Wrap around a list of 7 days(day + 1) % 7
Cents part of an amount in centscents % 100

The result takes the sign of the left operand: -7 % 3 is -1. That is why it is called the remainder, not the mathematical modulo.

Increment and Decrement

++ adds 1 to a variable and -- subtracts 1. Each has two forms, and they differ in the value the expression produces:

increment.js▶ Try it
let count = 5;
console.log(count++); // postfix: gives the old value, then adds 1
console.log(count);
console.log(++count); // prefix: adds 1, then gives the new value
console.log(count);
Output
5
6
7
7

Assignment Operators

= stores a value. Compound assignment operators combine an operation with storing:

OperatorExampleSame as
=x = 5
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5
**=x **= 2x = x ** 2

There are also logical assignment operators (||=, &&=, ??=), covered in the logical operators lesson.

assignment.js▶ Try it
let balance = 450;
balance -= 200;      // first payment
balance -= 150;      // second payment
balance *= 1.05;     // 5% late charge on what is left
console.log(balance);
Output
105

+ with Strings

The + operator does two jobs: it adds numbers and joins (concatenates) strings. If either operand is a string, it joins:

plus-strings.js▶ Try it
console.log(120 + 30);
console.log("120" + 30);
console.log(120 + "30");
console.log(1 + 2 + "3");
console.log("1" + 2 + 3);
console.log("120" - 30);
console.log("120" * 2);
Output
150
12030
12030
33
123
90
240

Explanation: operators run left to right. In 1 + 2 + "3", 1 + 2 is 3 first, then 3 + "3" joins to "33". In "1" + 2 + 3, the first step already makes a string, so everything after joins. The other operators (-, *, /) only work on numbers, so they convert strings to numbers: "120" - 30 is 90.

Division by Zero and NaN

JavaScript arithmetic never throws an error. Instead, it produces special number values:

special.js▶ Try it
console.log(120 / 0);
console.log(-120 / 0);
console.log(0 / 0);
console.log("abc" * 2);
console.log(Math.sqrt(-1));
Output
Infinity
-Infinity
NaN
NaN
NaN

NaN means "Not a Number": the result of math that has no meaningful number answer. It spreads: any arithmetic with NaN gives NaN. The numbers lesson shows how to test for it (Number.isNaN).

In Practice

The Fee Tracker works out a late fee: $2 per day late, capped at $30, and adds it to the balance. Amounts are kept in cents (whole numbers) so decimals don't cause rounding errors; the numbers chapter explains why.

late-fee.js▶ Try it
const balanceCents = 12000;      // $120.00
const daysLate = 18;
const ratePerDayCents = 200;     // $2.00
const capCents = 3000;           // $30.00
 
let lateFeeCents = daysLate * ratePerDayCents;
if (lateFeeCents > capCents) {
  lateFeeCents = capCents;
}
 
const totalCents = balanceCents + lateFeeCents;
console.log("Late fee: $" + lateFeeCents / 100);
console.log("Total due: $" + totalCents / 100);
Output
Late fee: $30
Total due: $150

Common Mistakes

Adding numbers that are really strings. "50" + "25" is "5025". Convert with Number() first.

Expecting integer division. 7 / 2 is 3.5. Use Math.floor(7 / 2) (or Math.trunc for negative numbers) when you need a whole number.

Writing x =+ 5. That is x = +5 (assign positive 5), not x += 5. Read compound operators carefully.

Using ^ for power. 2 ^ 3 is 1 (a bitwise operator). Power is 2 ** 3, which is 8.

Practice

Challenge: A family pays $1,350 a year in 12 equal monthly payments, rounded down to whole dollars, with the remainder added to the last month. Print the regular payment and the last payment.

Show solution
installments.js▶ Try it
const yearly = 1350;
const months = 12;
 
const regular = Math.floor(yearly / months);
const last = regular + (yearly % months);
 
console.log("Regular payment: $" + regular);
console.log("Last payment: $" + last);
Output
Regular payment: $112
Last payment: $118

Quick check: Arithmetic operators

4 questions
1What is "10" + 5?
2What is "10" - 5?
3What does let n = 3; console.log(n++); print?
4What is 10 / 0?

Key Takeaways

OperatorUse
%Remainder: even/odd, wrap-around, leftovers
**Power: 2 ** 10
+=Add to a running total
Number(x)Convert text to a number before adding

Was this tutorial helpful?

Lesson 8 of 139