4.2 Switch...case
JavaScript offers a second kind of conditional statement: switch / case. It is useful when you want to compare the value of a single variable against several specific values. Imagine a variable role that holds the role of the current user — guest, moderator, admin — and you want to print a different message in each case.
The switch / case syntax
let role;
switch (role) {
case 'guest':
console.log('Guest user');
break;
case 'moderator':
console.log('Moderator user');
break;
default:
console.log('Unknown user');
}
The variable to test goes between the parentheses of switch. Each case compares it against a specific value and runs the matching block. The break statement is essential: without it the execution would fall through into the next case. The optional default block runs when no case matches.
switch / case vs if / else if
- The same logic can be expressed with
if/else if/elseusing strict equality (===). - The
ifversion is often shorter and more readable because it avoids all thebreakstatements and braces. - Use
switchwhen you compare against many specific string or number values; useiffor range checks or boolean conditions.
That's it for switch / case. In the next demo we'll move on to for loops.
Summary
This lesson introduces the JavaScript switch...case statement, a conditional construct used to compare a variable's value against multiple cases. The instructor explains the syntax (switch keyword, case statements, break commands, and default fallback), demonstrates with a 'role' variable example (guest, moderator, admin), and compares switch to if/else if/else alternatives, emphasizing that switch is cleaner for multiple discrete values while if/else is more flexible.
Key points
- Switch statements compare a single variable against multiple discrete values using case blocks, each ending with break to prevent fall-through execution.
- The break statement is critical—omitting it causes subsequent cases to execute (fall-through behavior), though default does not require break since it exits automatically.
- Values in switch cases are not limited to strings; they can be numbers or booleans, though booleans are less common since if statements are more intuitive for true/false checks.
- Switch provides cleaner, more readable code than nested if/else if/else when testing one variable against many exact matches.
- The optional default case acts as a catch-all, executed when no case matches, similar to a final else block.
FAQ
What happens if I forget the break statement in a switch case?
Without break, execution continues to the next case (fall-through), executing subsequent instructions even if their conditions don't match. This is usually unintended and is why break is essential after each case block.
When should I use switch instead of if/else if statements?
Use switch when comparing a single variable against many discrete, exact values (especially strings or numbers). Use if/else if for complex conditions, boolean logic, or range comparisons. Switch is cleaner and more readable for simple equality checks.
Do I need break in the default case?
No, default does not require break because it's the final block—control automatically exits the switch after executing default instructions.