Quest 1 • Lesson 3
➕ JavaScript Operators & Expressions
Operators are the verbs of JavaScript. Learn how to do math, compare values, and combine conditions.
An operator performs an action on values (called operands). JavaScript has five main groups you'll use every day.
2 + 3 is an expression. 2 + 3 * 4 is also an expression — with a specific answer."
🔢 1. Arithmetic Operators
Basic math on numbers:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 15 / 3 | 5 |
% | Modulus (remainder) | 7 % 3 | 1 |
** | Exponent | 2 ** 3 | 8 |
console.log(5 + 3); // 8
console.log(10 - 4); // 6
console.log(3 * 4); // 12
console.log(20 / 5); // 4
console.log(7 % 3); // 1 (remainder of 7 ÷ 3)
console.log(2 ** 4); // 16 (2 to the power of 4)
💡 Modulus (%) is underrated. It's how you check if a number is even or odd: n % 2 === 0 means even.
⚖️ 2. Comparison Operators
Compare two values, always return true or false:
| Operator | Meaning | Example | Result |
|---|---|---|---|
=== | Strict equal | 5 === 5 | true |
!== | Strict not equal | 5 !== 3 | true |
> | Greater than | 10 > 5 | true |
< | Less than | 3 < 1 | false |
>= | Greater or equal | 5 >= 5 | true |
<= | Less or equal | 4 <= 3 | false |
⚠️ Always use === not ==. The double equals does "type coercion" and produces surprising results. "5" == 5 is true, but "5" === 5 is false. Stick with triple equals.
🧠 3. Logical Operators
Combine multiple conditions:
| Operator | Name | Meaning |
|---|---|---|
&& | AND | True only if both sides are true |
|| | OR | True if either side is true |
! | NOT | Flips true to false and vice versa |
console.log(true && true); // true
console.log(true && false); // false
console.log(true || false); // true
console.log(false || false); // false
console.log(!true); // false
console.log(!false); // true
💡 Real use case: if (age >= 18 && hasID) — both must be true for entry.
📝 4. Assignment Operators
Shortcuts for updating variables:
let score = 0;
score = 10; // simple assign
score += 5; // same as: score = score + 5 → 15
score -= 3; // same as: score = score - 3 → 12
score *= 2; // same as: score = score * 2 → 24
score /= 4; // same as: score = score / 4 → 6
console.log(score); // 6
📖 5. String Concatenation vs. Template Literals
You have two ways to combine strings with variables:
const name = "Alice";
const age = 25;
// Old way — string concatenation with +
console.log("My name is " + name + " and I'm " + age + ".");
// Modern way — template literal with backticks
console.log(`My name is ${name} and I'm ${age}.`);
Both produce the same output. But the second is cleaner, handles multi-line text easily, and lets you embed expressions directly:
const price = 12.5;
const qty = 3;
console.log(`Total: $${(price * qty).toFixed(2)}`); // Total: $37.50
💡 Always use template literals in modern JavaScript. String concatenation with + is fine, but backticks are easier to read and less error-prone.
🚀 Try It Live
Edit the code below and click Run to see the output.
🧮 Interactive Operator Playground
Enter two numbers and click any operator to see the result.
✨ Challenge: Even or Odd
Write code that takes a number and prints whether it's even or odd using the % operator.
📚 What's Next?
- Lesson 1.4: Conditional Statements → — control flow with if/else.
- ← Previous: Lesson 1.2 Variables & Data Types
- ⚡ JavaScript Course Hub →
❤️ Support Free Education
This course is 100% free. If it helps you, consider buying me a coffee.
☕ Buy Me a Coffee