Conditionals
Conditionals allow your code to take different paths depending on whether an expression evaluates to true or false.
Conditionals allow your code to take different paths depending on whether an expression evaluates to true or false.
const status = "success";
// Switch Statement with Block Scoping
switch (status) {
case "loading": {
const spinner = true;
break;
}
case "success": {
const data = fetchCached();
break;
}
default:
throw new Error(`Unhandled status: ${status}`);
}
// Optional Chaining with Nullish Coalescing
const street = user?.address?.street ?? "Unknown Street";
break statement causes execution to fall through into subsequent case statements regardless of whether their condition matched.let or const inside a switch statement share a single block scope unless wrapped in curly braces case "x": { const a = 1; break; }.obj?.method() prevents errors if obj is null/undefined, but if method exists and is NOT a function, it still throws a TypeError: obj.method is not a function.if (status = "admin") assigns the string instead of comparing it, mutating the variable and always evaluating to truthy.a ? b : c ? d : e ? f : g) obscures business logic. Flatten code using early returns or object strategy dictionaries.