Phase 1 · FoundationsModule 3~36 min read

Operators & Expressions

Combine values with arithmetic, comparison, logical, assignment, and modern null-safe operators.

What you'll learn

Operators are JavaScript's vocabulary for combining, comparing, and transforming values. The goal is not memorization—it is writing expressions whose meaning is obvious.

Expressions and statements

An expression produces a value: price * quantity, isReady && isOnline, or a function call. A statement performs an action: declaring a variable, returning, or choosing with if.

expressions.js
const price = 12;
const quantity = 3;
const subtotal = price * quantity; // expression on the right
console.log(subtotal);             // expression statement

Arithmetic and assignment

OperatorMeaningExample
+Addition (or string concatenation)5 + 2 → 7
- / * /Subtract, multiply, divide10 / 4 → 2.5
%Remainder10 % 3 → 1
**Exponentiation2 ** 3 → 8
+= -= *= /=Update assignmentscore += 10
math.js
let score = 10;
score += 5;

console.log(score);
console.log(17 % 5);
console.log(2 ** 8);

The + operator has two jobs

2 + 3 is numeric addition, but "2" + 3 produces "23". Convert inputs before doing arithmetic.

Comparison and strict equality

OperatorQuestion
=== / !==Same type and same value?
< <= > >=How are values ordered?
== / !=Equal after coercion? (usually avoid)
comparison.js
console.log(5 === 5);
console.log(5 === "5");
console.log(5 == "5");
console.log(12 >= 10);

Key idea

Use === and !== by default. Strict equality prevents hidden type conversion and makes comparisons easier to reason about.

Logical operators and short-circuiting

&& returns its first falsy operand or the final operand. || returns its first truthy operand or the final operand. ! negates after converting to boolean.

logic.js
const signedIn = true;
const hasAccess = false;

console.log(signedIn && hasAccess);
console.log(signedIn || hasAccess);
console.log(!signedIn);

const displayName = "" || "Guest";
console.log(displayName);

Because these operators return operands—not necessarily booleans—they are also useful for guards and defaults.

Nullish coalescing and optional chaining

?? uses its right side only when the left side is null or undefined. That preserves legitimate values such as 0, false, and an empty string. Optional chaining ?. stops safely when a receiver is nullish.

safe-access.js
const settings = { volume: 0, profile: null };

console.log(settings.volume || 50);       // 50 (often wrong)
console.log(settings.volume ?? 50);       // 0
console.log(settings.profile?.name ?? "Guest");

Precedence and associativity

Multiplication runs before addition, comparisons before logical AND, and assignment near the end. You do not need to memorize the complete table: use parentheses whenever a reader might hesitate.

precedence.js
console.log(2 + 3 * 4);       // 14
console.log((2 + 3) * 4);     // 20

const eligible = (age >= 18) && hasTicket;

Tip

Parentheses are communication, not just mathematics. (age >= 18) && hasTicket makes the intended grouping visible.

Recap & quick check

Key takeaways

  • Expressions produce values; statements direct program flow.
  • Use === and !== to avoid equality coercion.
  • && and || short-circuit and return operands.
  • Use ?? for nullish defaults and ?. for safe property access.
  • Add parentheses when operator grouping is not immediately obvious.

Quick check

1. What is 17 % 5?

2. What is the result of 5 === '5'?

3. Which default preserves a valid zero?

4. What does optional chaining prevent?