Conditionals
JavaScript · Reference cheat sheet
Conditionals
JavaScript · Reference cheat sheet
📋 Overview
Conditionals branch control flow with if / else if / else, switch, and short-circuit logic. Prefer clear boolean expressions, early returns, and exhaustive switches for unions. Truthiness rules matter — know what counts as falsy.
🔧 Core concepts
- Falsy:
false,0,-0,0n,"",null,undefined,NaN. - Truthy: everything else (including
[],\{\},"0"). if (expr): coerces with ToBoolean.else if/else: chained alternatives.switch (x): strict equality (===) match; rememberbreak.- Guards: early
return/throwto flatten nesting.
if (user?.isAdmin) {
allow();
} else if (user) {
deny();
} else {
login();
}💡 Examples
// Early return style
function canEdit(doc, user) {
if (!user) return false;
if (doc.locked) return false;
return doc.ownerId === user.id || user.role === "admin";
}
// switch with fall-through (intentional)
switch (code) {
case 200:
case 201:
return "ok";
case 404:
return "missing";
default:
return "error";
}
// switch (true) pattern
switch (true) {
case score >= 90:
return "A";
case score >= 80:
return "B";
default:
return "C";
}
// Short-circuit
isReady && start();
value || fallback; // prefer ?? for nullish-only
exists ? use(exists) : create();
// Numeric range
if (n >= 1 && n <= 10) {
/* ... */
}// Nullish check without truthiness
if (count != null) {
// allows 0
}⚠️ Pitfalls
if (arr)is always true for empty arrays — checkarr.length.- Assignment in condition
if (x = y)is legal and often a bug — use===. switchwithoutbreakfalls through — usebreakorreturn.switchuses===— no coercion between"1"and1.- Deep nesting hurts readability — extract functions / use lookup maps.
🔗 Related
- ternary.md — conditional expressions
- boolean.md — booleans
- equality.md — === vs ==
- switch_case.md — switch deep dive
- nullish_coalescing.md — defaults