Switch and Case: Named Alternatives
Choose a message from a club code
Students select a club code: art, music or sport. The school wants a different room instruction for each named choice, with a clear message for an unknown code.
A switch evaluates a value and selects a matching case. In JavaScript, case matching uses strict equality. Execution can continue into following cases unless a control statement stops it.
Match a case
Explain which club branch a value selects.
Prevent fall-through
Use break for separate alternatives and understand intentional grouped cases.
Handle unknowns
Provide a useful default and know when if is clearer.
Match music to its room
Read the code
The chosen value is music.
musicMatch a case
The music label equals the chosen value.
Room 2Leave the switch
Break prevents the next case from running.
Continue after switch
Cases match values rather than numeric ranges
Use switch when one value has several discrete alternatives. It is often clearer than repeating equality checks against the same club code. For mark ranges, an ordered if chain is usually more direct.
break leaves the switch. default supplies a fallback when no case matched. Matching text is case-sensitive, so “Music” and “music” are distinct unless input is deliberately normalized.
Before starting, review conditional branches. These examples use JavaScript; other languages can use different syntax and rules.
Send the student to the selected club room
The chosen value matches music. The program prints Room 2 and break exits the switch, skipping the sport and default instructions.
The art case is not executed simply because it appears first; switch selects the matching entry point.
JavaScript · local teaching example
const club = 'music';
switch (club) {
case 'art':
console.log('Room 1');
break;
case 'music':
console.log('Room 2');
break;
case 'sport':
console.log('Sports field');
break;
default:
console.log('Choose a listed club');
}Expected output
Room 2See what a missing break actually does
After a match, JavaScript executes following statements until it leaves the switch. It does not test every following label as a new condition. Missing break here prints both club messages.
Intentional fall-through can group multiple case labels before one shared action. Comment deliberate fall-through when readers might otherwise think it is a mistake.
JavaScript · local teaching example
const club = 'art';
switch (club) {
case 'art':
console.log('Room 1');
case 'music':
console.log('Room 2');
break;
}Expected output
Room 1
Room 2Apply the rule to a small change
Write a switch for an unknown club code science with art and music as the only listed choices.
Worked answer
No listed case matches science, so default prints Choose a listed club. It is better than leaving the student without instructions.
JavaScript · local teaching example
const club = 'science';
switch (club) {
case 'art': console.log('Room 1'); break;
case 'music': console.log('Room 2'); break;
default: console.log('Choose a listed club');
}Expected output
Choose a listed clubRecognize mistakes and limits
A switch case with numeric 1 does not match string “1” under JavaScript strict equality. Normalize input deliberately rather than expecting a match.
Use braces around a case’s declarations when separate cases need their own lexical scope. JavaScript switch is not identical to pattern matching in other languages. Do not use it to obscure range checks or complicated combined conditions.
For the language or tool rules used here, see JavaScript control flow.
Check the core idea
JavaScript · code for this question
const club = 'music';
switch (club) {
case 'art':
console.log('Room 1');
break;
case 'music':
console.log('Room 2');
break;
case 'sport':
console.log('Sports field');
break;
default:
console.log('Choose a listed club');
}Apply the idea to a new situation
JavaScript · code for this question
const club = 'art';
switch (club) {
case 'art':
console.log('Room 1');
case 'music':
console.log('Room 2');
break;
}Match a value and control where execution stops
Switch is useful for named alternatives. Use break for separate cases, default for unmatched input, and intentional fall-through only when it makes the rule clearer.
Connect this idea with strict equality and range-based decisions.
Reaching the recap and completing the quizzes are separate milestones.