Operators (Arithmetic, Assignment, Comparison)
Operators perform operations on values — math, comparisons, and assignments. Master these operators to write any calculation, condition, or value manipulation in JavaScript.
JavaScript Operators
- Arithmetic: + - * / % ** — Addition, subtraction, multiply, divide, remainder, power
- Assignment: = += -= *= /= %= — Assign and update shorthand
- Comparison: == === != !== > < >= <= — Compare values, return boolean
- Increment/Decrement: ++x (pre) x++ (post) --x x-- — Add/subtract 1
- % (Modulus) — Remainder after division: 10 % 3 = 1. Check even: n % 2 === 0
- ** (Exponent) — Power: 2 ** 3 = 8 (same as Math.pow(2, 3))
- Always use === (strict equality) — checks both value AND type
Operators Code
// Arithmetic
let a = 10, b = 3;
console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.333...
console.log(a % b); // 1 (remainder)
console.log(a ** 2); // 100 (10 squared)
// Assignment shorthand
let score = 100;
score += 10; // score = 110
score -= 5; // score = 105
score *= 2; // score = 210
console.log(score);
// Increment / Decrement
let count = 0;
count++; // count = 1
count++; // count = 2
count--; // count = 1
// Comparison (always returns true/false)
console.log(10 > 5); // true
console.log(10 === 10); // true
console.log(10 === "10"); // false (strict!)
console.log(10 !== "10"); // trueTip
Tip
Use shorthand operators (+=, -=, *=) to make your code cleaner. Instead of score = score + 10, write score += 10. It's shorter, less error-prone, and the standard convention in professional JavaScript.
Use const by default, let when re-assignment is needed, never var
Common Mistake
Warning
Confusing = (assignment) with === (comparison). Writing if (x = 5) assigns 5 to x and always evaluates as truthy — it doesn't compare. Always use === for comparisons inside conditions.
Practice Task
Note
Build a simple calculator in the console: (1) Create two variables a = 15 and b = 4. (2) Log results of all arithmetic operations (+, -, *, /, %, **). (3) Use comparison operators to check if a is greater, equal, or less than b.
Quick Quiz
Key Takeaways
- Operators perform operations on values — math, comparisons, and assignments.
- Arithmetic: + - * / % ** — Addition, subtraction, multiply, divide, remainder, power
- Assignment: = += -= *= /= %= — Assign and update shorthand
- Comparison: == === != !== > < >= <= — Compare values, return boolean