Unit 3 of 20

Operators & expressions (arithmetic, comparison, logical)

Updated Jun 2026

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.

Concept 1 — Arithmetic operators

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)

Concept 2 — Comparison operators

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 considers 5 == "5" to be true, which causes subtle bugs. Triple equals checks both value and type.

Concept 3 — Logical operators

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.");
}

Concept 4 — String operators

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.

Concept 5 — The ternary operator

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.

Try it yourself

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?

Quick reference

OperatorTypeExampleResult
+ - * /Arithmetic10 + 313
%Remainder10 % 31
**Exponent2 ** 8256
++ --Increment/decrementx++x + 1
=== !==Strict equality5 === "5"false
> < >= <=Comparison5 >= 5true
&&Logical ANDtrue && falsefalse
||Logical ORtrue || falsetrue
!Logical NOT!truefalse
? :Ternaryx > 0 ? "yes" : "no"conditional