Chapter 11 of 16All chapters
Chapter 11 of 16
Conditions and loops
Branching, iterating and stopping early.
Branching
if, else if and else cover most decisions, and switch suits a value with several known cases. The ternary a ? b : c is an expression, so it fits where a statement cannot go.
- The ?? operator returns the right side only for null and undefined, unlike || which also fires on 0 and "".
- Assign with ??= to fill in a missing value.
Loops
for...of walks values in an array or any iterable, for...in walks keys of an object, and while repeats until a condition fails. break leaves a loop and continue skips to the next round.