Default Case

Handling no match.

Examples

Using Default

Code runs if no case matches.

let text;
switch (new Date().getDay()) {
  case 6:
    text = "Today is Saturday";
    break;
  case 0:
    text = "Today is Sunday";
    break;
  default:
    text = "Looking forward to the Weekend";
}
console.log(text);

Default Position

Default does not have to be the last case (but requires break if not).

switch (day) {
  default:
    text = "Looking forward to the Weekend";
    break;
  case 6:
    text = "Today is Saturday";
    break;
  case 0:
    text = "Today is Sunday";
}

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.