JavaScript Switch Statement
Let us now map some activities to each day using switch statement
In the real world, it does not have to be hard coded like this because usually they require user input.
if / else statement with many blocks could also be used but this switch statement is much easier way.
As you can see the variable ‘day’ is set to Monday initially, it displays Monday’s console log and the default at the end is more like else block when nothing matches the case.
What does each ‘Break’ do?
Let us comment out the break then, it displays like this
Basically, each break stops when the case is done. As you can see above, break on Monday is commented out and even though Monday case is done it still continues to next break where it is at the end of Tuesday. Therefore, break is essential for each case.