Operators let you do things with values — compare them, combine them, and make decisions based on them. An expression is any piece of code that produces a value.
You’ve seen these in maths. They work the same way in JavaScript.
console.log(10 + 3); // 13 — addition
console.log(10 - 3); // 7 — subtraction
console.log(10 * 3); // 30 — multiplication
console.log(10 / 4); // 2.5 — division
console.log(10 % 3); // 1 — remainder (modulo)
console.log(2 ** 8); // 256 — exponentiation
Shorthand assignment operators — a common way to update a variable in place:
let score = 10;
score += 5; // score is now 15 (same as score = score + 5)
score -= 3; // score is now 12
score *= 2; // score is now 24
score /= 4; // score is now 6
score++; // score is now 7 (increment by 1)
score--; // score is now 6 (decrement by 1)
Comparison operators compare two values and return a boolean (true or false).
console.log(5 > 3); // true — greater than
console.log(5 < 3); // false — less than
console.log(5 >= 5); // true — greater than or equal
console.log(5 <= 4); // false — less than or equal
console.log(5 === 5); // true — strictly equal
console.log(5 !== 3); // true — strictly not equal
Always use
===and!==(triple equals). The double equals==exists but performs type coercion — it considers5 == "5"to betrue, which causes subtle bugs. Triple equals checks both value and type.
Logical operators combine or invert boolean values.
const isLoggedIn = true;
const hasAccess = false;
// AND — both must be true
console.log(isLoggedIn && hasAccess); // false
// OR — at least one must be true
console.log(isLoggedIn || hasAccess); // true
// NOT — inverts the boolean
console.log(!isLoggedIn); // false
A real-world example:
const age = 20;
const hasTicket = true;
if (age >= 18 && hasTicket) {
console.log("Entry granted.");
}
The + operator doubles as string concatenation. When used with at least one string, it joins values together.
const first = "Hello";
const second = "world";
console.log(first + ", " + second + "!"); // Hello, world!
Template literals (backticks) are the modern, cleaner alternative — no + needed:
const name = "Sam";
const age = 28;
console.log(`${name} is ${age} years old.`); // Sam is 28 years old.
The ternary operator is a compact way to write a simple if/else on one line.
condition ? valueIfTrue : valueIfFalse
const age = 20;
const status = age >= 18 ? "adult" : "minor";
console.log(status); // adult
It reads as: “if age is 18 or over, status is ‘adult’, otherwise it’s ‘minor’.” Use it for simple choices — for anything more complex, a full if statement is clearer.
let price = 100;
const discount = 20;
const isMember = true;
price -= discount;
console.log(`Price after discount: $${price}`);
const finalPrice = isMember ? price * 0.9 : price;
console.log(`Final price: $${finalPrice}`);
console.log(finalPrice < 100); // true or false?
| Operator | Type | Example | Result |
|---|---|---|---|
+ - * / | Arithmetic | 10 + 3 | 13 |
% | Remainder | 10 % 3 | 1 |
** | Exponent | 2 ** 8 | 256 |
++ -- | Increment/decrement | x++ | x + 1 |
=== !== | Strict equality | 5 === "5" | false |
> < >= <= | Comparison | 5 >= 5 | true |
&& | Logical AND | true && false | false |
|| | Logical OR | true || false | true |
! | Logical NOT | !true | false |
? : | Ternary | x > 0 ? "yes" : "no" | conditional |