Switch Statement in JavaScript

The switch statement is a powerful control flow structure in JavaScript used for conditional execution, similar to if-else statements. It evaluates an expression and matches the result against multiple case values. Switch is often preferred over multiple if-else statements for better performance and readability when dealing with many conditions. The break keyword is crucial to prevent fall-through to subsequent cases.


How to Use Switch in JavaScript

The switch statement in JavaScript provides a clean way to handle multiple conditional branches. The expression inside the switch parentheses is evaluated once, and its value is compared with the values of each case. When a match is found, the associated code block executes.

The Break Keyword

The break statement is essential after each case block. Without it, execution would continue to the next case (fall-through), which is sometimes intentional but often leads to bugs. Always include break unless you specifically want fall-through behavior.

The Default Case

The default case executes when none of the case values match the switch expression. It's optional but recommended for handling unexpected values gracefully.

Switch Example


       const x=2;

        switch(x){
            case 1: output; break;
            case 2: output; break;
            default : output; break; 
        }
        

Check Number using Switch

Its One


      const x=1;
      switch(x){
        case 1 : console.log("Its One"); break;
        case 2 : console.log("Its Two"); break;
        case 3 : console.log("Its Three"); break;
        case 4 : console.log("Its Four"); break;
        case 5 : console.log("Its Five"); break;
        case 6 : console.log("Its six"); break;
        case 7 : console.log("Its seven"); break;
        default : console.log("Invalid Number");
      }
        

Switch Case Example

Check month name by using number.

:


      const console.logmonth=n;  // from input above
      
      switch(month){
      case "1" : console.log("Its Jan"); break;
      case "2" : console.log("Its Feb"); break;
      case "3" : console.log("Its Mar"); break;
      case "4" : console.log("Its Apr"); break;
      case "5" : console.log("Its May"); break;
      case "6" : console.log("Its June"); break;
      case "7" : console.log("Its July"); break;
      case "8" : console.log("Its Aug"); break;
      case "9" : console.log("Its Sept"); break;
      case "10" : console.log("Its Oct"); break;
      case "11" : console.log("Its Nov"); break;
      case "12" : console.log("Its Dec"); break;
      default : console.log("invalid month");
      }
        

Multiple Cases in Switch

Multiple cases can share the same code block in a switch statement. This is useful for grouping similar conditions. Instead of repeating code, you can list multiple case labels before the code block. This technique is efficient and keeps your code DRY (Don't Repeat Yourself).


const data = 3;

switch(data){
  case 1:
  case 3:
  case 5:
    console.log("Odd number");
    break;
  case 2:
  case 4:
  case 6:
    console.log("Even number");
    break;
  default:
    console.log("Number not in range");
}    

In this example, if data is 1, 3, or 5, it will log "Odd number". Notice how the break is only at the end of the shared block.

Switch with Strings

Switch statements work with strings as well as numbers. This is useful for handling text-based conditions.


const fruit = "apple";

switch(fruit){
  case "apple":
    console.log("Apples are red or green");
    break;
  case "banana":
    console.log("Bananas are yellow");
    break;
  case "orange":
    console.log("Oranges are orange");
    break;
  default:
    console.log("Unknown fruit");
}