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
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Add | 25 + 15 | 40 |
- | Subtract | 100 - 45 | 55 |
* | Multiply | 12 * 3 | 36 |
/ | Divide | 10 / 4 | 2.5 |
% | Remainder (modulo) | 10 % 3 | 1 |
** | Power | 2 ** 5 | 32 |
<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 % 2is0for even numbers and1for odd numbers.- If 43 students form groups of 5,
43 % 5is3: three students are left without a full group. - To convert 135 minutes:
Math.floor(135 / 60)is 2 hours, and135 % 60is 15 minutes.
<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:
| Shortcut | Same as |
|---|---|
x += 5 | x = x + 5 |
x -= 5 | x = x - 5 |
x *= 2 | x = x * 2 |
x /= 2 | x = x / 2 |
x++ | x = x + 1 |
x-- | x = x - 1 |
<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. 🙂
By Sir Reyel · Last updated 2026-12-04