Skip to main content

break in switch

The break statement in a switch construct is used to interrupt execution of the switch block, so the program does not continue into the following cases after a match is found.


Example without break

javascript
const day = 2; switch (day) { case 1: console.log('Monday'); case 2: console.log('Tuesday'); case 3: console.log('Wednesday'); default: console.log('Unknown day'); }

Result:

javascript
Tuesday Wednesday Unknown day

After matching case 2, the code did not stop, it "fell through" downward, running all the following blocks. This behavior is called "fall-through".


Example with break

javascript
const day = 2; switch (day) { case 1: console.log('Monday'); break; case 2: console.log('Tuesday'); break; case 3: console.log('Wednesday'); break; default: console.log('Unknown day'); }

Result:

javascript
Tuesday

After running console.log('Tuesday'), the break statement exited the switch, and the remaining cases were skipped.


When break is not needed

If you deliberately want to combine several cases:

javascript
const day = 6; switch (day) { case 6: case 7: console.log('Day off'); break; default: console.log('Workday'); }

Result:

javascript
Day off

Here, "falling through" is used intentionally, so both branches run the same code.


Summary

SituationWhat break does
After a case matchStops execution of the switch
If break is missingThe code "falls through" into the next case
In defaultUsually a trailing break is added, but it can be omitted
Can be used in other loopsStops for, while, do...while

In short

  • break in switch stops execution after the matching case.
  • Without it, all the following cases run until the end or until a break.
  • It is used to avoid "falling through".

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.