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

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

JavaScript operators and operator precedence โ€” JavaScript Foundations โ€” JavaScript: The Complete Language โ€” Script Valley โ€” Script Valley