JavaScript Switch Statements

JavaScript is a versatile programming language that offers various control structures to handle different scenarios. One of these control structures is the switch statement, which allows you to perform different actions based on different conditions. In this article, we will explore the switch statement in JavaScript and provide examples to help you understand its usage.

What is a Switch Statement?

A switch statement is a control structure in JavaScript that allows you to execute different blocks of code based on different cases. It provides an alternative to using multiple if-else statements when you have a large number of conditions to evaluate.

The syntax of a switch statement is as follows:

switch (expression) {
  case value1:
    // code to be executed if expression matches value1
    break;
  case value2:
    // code to be executed if expression matches value2
    break;
  case value3:
    // code to be executed if expression matches value3
    break;
  default:
    // code to be executed if expression doesn't match any case
    break;
}

Here’s how the switch statement works:

  1. The expression is evaluated once.
  2. The value of the expression is compared to the values of each case.
  3. If a match is found, the code block associated with that case is executed.
  4. If no match is found, the code block associated with the default case (if present) is executed.
  5. The break statement is used to exit the switch statement once a match is found. Without the break statement, execution will continue to the next case, resulting in unexpected behavior.

Example 1: Days of the Week

Let’s start with a simple example to understand how the switch statement works. Consider a scenario where you want to display a message based on the day of the week:

let day = "Monday";
let message;

switch (day) {
  case "Monday":
    message = "It's the start of the week!";
    break;
  case "Tuesday":
  case "Wednesday":
  case "Thursday":
    message = "It's a weekday.";
    break;
  case "Friday":
    message = "TGIF!";
    break;
  case "Saturday":
  case "Sunday":
    message = "It's the weekend!";
    break;
  default:
    message = "Invalid day.";
    break;
}

console.log(message);

In this example, the switch statement evaluates the value of the variable “day” and executes the corresponding code block based on the matching case. In this case, the output will be “It’s the start of the week!” since the value of “day” is “Monday”.

Example 2: Grades

Let’s consider another example where you want to assign a grade based on a student’s score:

let score = 85;
let grade;

switch (true) {
  case (score >= 90):
    grade = "A";
    break;
  case (score >= 80):
    grade = "B";
    break;
  case (score >= 70):
    grade = "C";
    break;
  case (score >= 60):
    grade = "D";
    break;
  default:
    grade = "F";
    break;
}

console.log("Your grade is " + grade);

In this example, the switch statement evaluates the condition “true” and compares it with each case. The code block associated with the first matching case is executed. In this case, the output will be “Your grade is B” since the value of “score” is 85.

Conclusion

The switch statement in JavaScript provides a concise and efficient way to handle multiple conditions. It allows you to execute different blocks of code based on different cases, providing an alternative to using multiple if-else statements. By understanding the syntax and examples provided in this article, you can effectively utilize the switch statement in your JavaScript code.

Scroll to Top