Script Valley
JavaScript: The Complete Language
JavaScript FoundationsLesson 1.4

JavaScript operators and operator precedence

arithmetic operators, comparison operators, logical operators, nullish coalescing, optional chaining, ternary operator, short-circuit evaluation, operator precedence

Operators Are the Grammar of Expressions

JavaScript operator categories diagram

An operator takes one or more values and produces a new value. Mastering operators lets you write compact, readable expressions.

Arithmetic and Comparison

// arithmetic
5 + 3   // 8
10 % 3  // 1  (remainder)
2 ** 8  // 256 (exponent)

// comparison - always use ===
5 === 5    // true
5 === "5"  // false  (different types)
5 == "5"   // true   ← coerces types, avoid

Logical Operators and Short-Circuit

Logical operators don't always return true/false - they return one of their operands.

const a = null;
const b = "default";

a || b   // "default"  - returns b because a is falsy
a && b   // null       - returns a because a is falsy
a ?? b   // "default"  - nullish coalescing: only null/undefined triggers

Optional Chaining and Ternary

const user = null;
user?.profile?.avatar  // undefined - no error thrown

const role = isAdmin ? "admin" : "viewer";

Precedence

Multiplication binds tighter than addition: 2 + 3 * 4 is 14. When in doubt, use parentheses to make intent explicit - it costs nothing and eliminates ambiguity.

Up next

Control flow: if, else, switch, and loops in JavaScript

Sign in to track progress