The switch Keyword

This lesson is about how the switch conditional expression handles different conditions.

What is the switch Keyword?

The switch keyword allows us to specify the different actions the program can perform based on the value of a particular expression.

In an if-else expression, the condition always returns a boolean. This means that it can only cater for two cases: true and false.

With a switch keyword, the conditional expression is not restricted to booleans. Hence, we can define the behavior of the program for several cases. These cases are separated by the pipe operator, |.

The Structure

Here’s the template for a typical switch conditional expression.

Get hands-on with 1200+ tech skills courses.