JavaScript Switch Statement

Benny Kang
2 min readNov 1, 2021

--

Let us now map some activities to each day using switch statement

Example of 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.

Output in console

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?

Comment out the break

Let us comment out the break then, it displays like this

Output

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.

--

--

No responses yet