JavaScript(JS) switch Statement

switch Statement

In JavaScript, the switch statement is used to execute different actions based on different conditions. It is often used as an alternative to if...else if...else statements when there are many different possible values for a variable. Here is the basic syntax of a switch statement:

ref‮gi:ot re‬iftidea.com
switch (expression) {
  case value1:
    // code block to be executed when expression matches value1
    break;
  case value2:
    // code block to be executed when expression matches value2
    break;
  ...
  default:
    // code block to be executed when none of the above cases are true
}

The expression is evaluated once and the value is compared with each case value. If there is a match, the code block associated with that case statement is executed. The break statement is used to terminate the switch statement and to prevent the execution of the next case statement. If no case values match the expression, the code block associated with the default statement is executed.

Here's an example that uses a switch statement to print the day of the week based on a numeric input:

let day = 5;
let dayName;

switch (day) {
  case 1:
    dayName = "Sunday";
    break;
  case 2:
    dayName = "Monday";
    break;
  case 3:
    dayName = "Tuesday";
    break;
  case 4:
    dayName = "Wednesday";
    break;
  case 5:
    dayName = "Thursday";
    break;
  case 6:
    dayName = "Friday";
    break;
  case 7:
    dayName = "Saturday";
    break;
  default:
    dayName = "Invalid day";
}

console.log(dayName); // Output: Thursday

In this example, the day variable is assigned the value 5. The switch statement then checks the value of day against each case value. Since day is equal to 5, the code block associated with the case 5 statement is executed, assigning the value "Thursday" to the dayName variable. Finally, the value of dayName is logged to the console.

JavaScript switch With Multiple Case

In JavaScript, you can use the switch statement to execute different code blocks based on different conditions.

Here's an example of using the switch statement with multiple case statements:

let day = "Monday";
switch (day) {
  case "Monday":
  case "Tuesday":
  case "Wednesday":
  case "Thursday":
  case "Friday":
    console.log("It's a weekday.");
    break;
  case "Saturday":
  case "Sunday":
    console.log("It's a weekend day.");
    break;
  default:
    console.log("Not a valid day.");
}

In this example, we have a variable called day that is set to "Monday". We use the switch statement to check the value of the day variable, and depending on the value, we execute different code blocks.

If the value of day is "Monday", "Tuesday", "Wednesday", "Thursday", or "Friday", we print "It's a weekday." to the console. If the value of day is "Saturday" or "Sunday", we print "It's a weekend day." to the console. If the value of day is anything else, we print "Not a valid day." to the console.

Note that we can use multiple case statements for a single code block, as we did for the weekdays. We also have a default statement, which is executed if none of the case values match the value of the variable being checked.