[JavaScript] Switch Statement

Note: Case values are tested with strict equality (===)

/* Default Option in Switch Statements */
var result = "";
switch (num) {
  case 1:
    result = "1";
    break;
  case 2:
    result = "2";
    break;
  default:
    result = "3 or more";
    break;
}

/* Multiple Identical Options in Switch Statements */
var result = "";
switch(val) {
  case 1:
  case 2:
  case 3:
    result = "1, 2, or 3";
    break;
  case 4:
    result = "4 alone";
}

Leave a Reply

Your email address will not be published. Required fields are marked *