Menu
Coddy logo textTech

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 expression is 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 break statement is used to exit the switch statement. Without it, execution would continue to the next case.
  • The default case 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: Tuesday

In 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 icon

Challenge

Beginner

You 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 expression is evaluated once and compared with each case value
  • The break statement exits the switch; without it, execution continues to the next case
  • The default case 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: Tuesday

Try 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);
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals