The switch statement is used to perform different actions based on different
conditions. It is a more organized alternative to using multiple if...else if
statements when you have many potential matches for a single value.
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
break keyword breaks out of the switch block and
stops the execution.default keyword specifies the code to run if there
is no case match.When JavaScript reaches a break keyword, it breaks out of the switch block.
This will stop the execution inside the block. If you forget to use break,
the next case will be executed even if the evaluation does not match the case (this is
called "fall-through").
Sometimes you will want different switch cases to use the same code. You can group them like this:
switch (new Date().getDay()) {
case 4:
case 5:
text = "Soon it is Weekend";
break;
case 0:
case 6:
text = "It is Weekend";
break;
default:
text = "Looking forward to the Weekend";
}
switch for multi-way branching based on a single
valuebreak to avoid fall-through
default case handles unsupported values (fallback)