Aral Code

Variables & Data Lesson 7 of 30

JavaScript Operators — Arithmetic and Assignment

Do math in JavaScript with +, -, *, /, % and **, update variables with +=, -= and ++, and understand the order of operations.

Programs compute constantly: totals, averages, change, discounts. Operators are the symbols that do the math. Most of them work just like in your Math class.

Arithmetic operators

OperatorMeaningExampleResult
+Add25 + 1540
-Subtract100 - 4555
*Multiply12 * 336
/Divide10 / 42.5
%Remainder (modulo)10 % 31
**Power2 ** 532
Example A sari-sari store receipt
<pre id="out"></pre>
<script>
  const noodles = 15 * 3;   // 3 packs of noodles
  const eggs = 9 * 6;       // 6 eggs
  const total = noodles + eggs;
  const payment = 200;
  const change = payment - total;

  document.getElementById("out").textContent =
    "Noodles: ₱" + noodles + "\n" +
    "Eggs:    ₱" + eggs + "\n" +
    "Total:   ₱" + total + "\n" +
    "Bayad:   ₱" + payment + "\n" +
    "Sukli:   ₱" + change;
</script>

The remainder operator %

% gives what's left over after dividing. It's more useful than it looks:

  • n % 2 is 0 for even numbers and 1 for odd numbers.
  • If 43 students form groups of 5, 43 % 5 is 3: three students are left without a full group.
  • To convert 135 minutes: Math.floor(135 / 60) is 2 hours, and 135 % 60 is 15 minutes.
Example Even or odd?
<p id="out"></p>
<script>
  const n = 27;
  document.getElementById("out").textContent =
    n + " % 2 = " + (n % 2) + ", so " + n + " is " + (n % 2 === 0 ? "even" : "odd") + ".";
</script>

Change 27 to other numbers and run it again.

Order of operations

JavaScript follows the same rules as math (PEMDAS): parentheses first, then powers, then multiplication and division, then addition and subtraction.

let a = 2 + 3 * 4;     // 14, not 20
let b = (2 + 3) * 4;   // 20
let average = (88 + 91 + 85) / 3;   // 88

Assignment operators

These are shortcuts for updating a variable:

ShortcutSame as
x += 5x = x + 5
x -= 5x = x - 5
x *= 2x = x * 2
x /= 2x = x / 2
x++x = x + 1
x--x = x - 1
Example A score counter
<p>Score: <strong id="score">0</strong></p>
<button onclick="add(1)">+1 (free throw)</button>
<button onclick="add(2)">+2</button>
<button onclick="add(3)">+3 (three-point)</button>
<script>
  let score = 0;
  function add(points) {
    score += points;
    document.getElementById("score").textContent = score;
  }
</script>

+ with strings

When either side of + is a string, + joins instead of adding. This is called concatenation:

"Juan" + " " + "Dela Cruz"   // "Juan Dela Cruz"
"Grade " + 10                // "Grade 10"
"1" + 2 + 3                  // "123"
1 + 2 + "3"                  // "33"  (1 + 2 first, then joined)

+= also works with text: message += "!"; adds an exclamation mark to the end.

Decimals can be slightly off

Computers store decimals in binary, so 0.1 + 0.2 gives 0.30000000000000004. For money, round the result with toFixed(2), which you'll learn in the Numbers lesson.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What is 17 % 5?
2. What is 2 + 3 * 4?
3. After let x = 10; x += 5;, what is x?
4. What does "Grade " + 7 + 3 give?

By Sir Reyel · Last updated 2026-12-04