JavaScript Tutorial: Operators & Control Flow
Make decisions with if/else, switch, ternary, and JavaScript's powerful nullish coalescing and optional chaining operators.
Give your program a brain
So far your programs run the same code every time. Control flow lets you make decisions — run this code *only if* the user is logged in, *switch* behaviour based on a role, *skip* a step if the value is null.
JavaScript has some powerful modern operators you won't find in older languages: nullish coalescing `??` and optional chaining `?.` are game-changers for handling missing data.
What you'll learn in this JavaScript operators & control flow tutorial
This interactive JavaScript tutorial has 9 hands-on exercises. Estimated time: 20 minutes.
- if / else — the basic decision — You know what if/else does — but JavaScript's version has a few quirks worth knowing.
- Logical operators: &&, ||, ! — Combine conditions:
- Short-circuit evaluation — JavaScript evaluates `&&` and `||` lazily — it stops as soon as it knows the answer:
- ?? — nullish coalescing — `||` has a problem: it treats ALL falsy values as 'no value'. That means `0 || 100` gives 100 — even though 0 is a valid…
- ?. — optional chaining — Accessing a property on `null` or `undefined` crashes with a TypeError. Optional chaining `?.` returns `undefined` inste…
- Ternary — one-line if/else — The ternary operator is a compact if/else that returns a value:
- switch — match exact values — `switch` is cleaner than a long if/else chain when matching exact values.
- Logical assignment operators — Modern JavaScript has shorthand for common patterns:
- Build an access control system — Final challenge — no starter code.
JavaScript Operators & Control Flow concepts covered
- Give your program a brain