ArticleZip > Javascript Typescript Switch Statement Way To Run Same Code For Two Cases

Javascript Typescript Switch Statement Way To Run Same Code For Two Cases

When working with JavaScript or TypeScript, utilizing the switch statement can be a powerful way to control the flow of your code based on different conditions. One common scenario developers face is the need to run the same block of code when two cases match. In this article, we'll explore how you can achieve this efficiently.

To run the same code for two cases in a switch statement, you can simply group the cases together. By listing multiple case labels followed by a colon and the code block you want to execute, you can ensure that the same block of code runs for those cases. Let's dive into an example to make things clearer.

Javascript

let fruit = "apple";

switch (fruit) {
  case "apple":
  case "banana":
    console.log("Both are delicious fruits!");
    break;
  case "orange":
    console.log("Oranges are juicy!");
    break;
  default:
    console.log("You are not hungry!");
}

In the example above, if the `fruit` variable is set to either "apple" or "banana", the code block inside the first two cases will execute, and you will see the message "Both are delicious fruits!" printed to the console. This technique allows you to keep your code concise and avoids repetition when handling multiple similar cases.

Remember, the `break` statement is crucial when using switch cases. It helps to exit the switch block after a case is executed, preventing the subsequent case blocks from executing unintentionally. If you forget to include the `break` statement, the control flow will "fall through" to the next case, which may lead to unexpected behavior in your code.

However, what if you want to include some additional logic for one of the cases while still running the common code block? You can achieve this by placing the specific logic inside the case block before the common code section and using `break` to exit the switch statement after both parts of the code are executed.

Javascript

let day = "Monday";

switch (day) {
  case "Monday":
    console.log("It's the start of the week!");
    // Additional logic specific to Monday goes here
    break;
  case "Friday":
  case "Saturday":
    console.log("It's time to relax!");
    break;
  default:
    console.log("Just another day!");
}

In this updated example, when `day` is set to "Monday," you will see specific information related to the start of the week before the general message. For "Friday" or "Saturday," the relaxing message will be displayed, executing the common code for those days.

By leveraging the switch statement's ability to group cases together, you can streamline your code and handle multiple scenarios efficiently. Remember to use `break` appropriately to control the flow and ensure that your code behaves as expected. Next time you find yourself needing to run the same code for two cases, reach for the switch statement and simplify your logic.

×