Javascript - Switch Statement

About

Example

If you omit break from a case, and that case matches or runs, execution will continue with the next case.

  • Without breal
color = "green"
switch (color) { 
    case "blue": 
         console.log("blue");
    case "green": 
          console.log("green");
    case "red": 
         console.log("red");
    default:
         console.log("default");
}
  • With break
color = "green"
switch (color) { 
    case "blue": 
         console.log("blue");
         break;
    case "green": 
          console.log("green");
          break;
    case "red": 
          console.log("red");
          break;
    default:
         console.log("default");
}

Powered by ComboStrap