Switch Statement
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 27 of 77.
The switch statement is another way to control the flow of your program based on different conditions. It's similar to using if-else if-else, but it can be more concise and readable in certain situations.
Here's how the switch statement works:
switch (expression) {
case value1:
// Code to execute if expression matches value1
break;
case value2:
// Code to execute if expression matches value2
break;
// ... more cases ...
default:
// Code to execute if no case matches
}- The
expressionis evaluated once. - The value of the expression is compared with the values of each
case. - If there is a match, the associated block of code is executed.
- The
breakstatement is used to exit theswitchstatement. Without it, execution would continue to the next case.
- The
defaultcase is optional. It's executed if no other case matches.
Here's an example:
let day = 3;
let dayName;
switch (day) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
// ... cases for other days ...
default:
dayName = "Invalid day";
}
console.log(dayName); // Output: TuesdayIn this example, the switch statement checks the value of day. Since day is 3, the case for 3 is executed, setting dayName to "Tuesday".
Challenge
BeginnerYou will receive a command from the user.
Use a switch statement to set the value of the variable message based on the command.
Set message to:
"System starting"→ if the command is"start""System stopping"→ if the command is"stop""System paused"→ if the command is"pause""System resuming"→ if the command is"resume"
If the command doesn't match any known option, set:
"Unknown command"
At the end, the program will print the value of message.
Your job is to correctly set the value of <strong>message</strong> using a switch statement.
Cheat sheet
The switch statement controls program flow based on different conditions, similar to if-else if-else but more concise for certain situations.
Basic syntax:
switch (expression) {
case value1:
// Code to execute if expression matches value1
break;
case value2:
// Code to execute if expression matches value2
break;
default:
// Code to execute if no case matches
}- The
expressionis evaluated once and compared with eachcasevalue - The
breakstatement exits the switch; without it, execution continues to the next case - The
defaultcase is optional and executes if no other case matches
Example:
let day = 3;
let dayName;
switch (day) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
default:
dayName = "Invalid day";
}
console.log(dayName); // Output: TuesdayTry it yourself
// Get the command from the user
let command = inp;
// Create a variable to store the message
let message;
switch (command) {
// Write your code here
}
// Print the message
console.log(message);This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False