JavaScript - Switch Case

The switch statement in JavaScript is used to perform multiple tests based on a single expression. It is an alternative to using multiple if...else statements, especially when you have many conditions to check.

Syntax:

switch (expression) {

  case value1:

    // code to be executed if expression matches value1

    break;

  case value2:

    // code to be executed if expression matches value2

    break;

  ...

  default:

    // code to be executed if expression doesn't match any of the values

}