Open main menu

The case keywordEdit

The case keyword is used in the switch statement that is an alternative for complex if … else if … else statements.

ExamplesEdit

  The code
  var array = [2, 3, 5, 7, 10, 11];

  for (var i = 0; i < array.length; i++) {
    switch (array[i]) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
      alert(array[i] + " is a number with one digit.");
      break;
    default:
      alert(array[i] + " is a number with several digits.");
      break;
    }
  }
returns the following:
2 is a number with one digit.
3 is a number with one digit.
5 is a number with one digit.
7 is a number with one digit.
10 is a number with several digit.
11 is a number with several digit.


See alsoEdit