When you have a number of conditions to evaluate for a given expression, using an if else
might not be the best method.
Let us look at an example where, based on the value of the variable languageCode
, we print the name of the language.
As you can see, the switch case method seems more readable when there are a lot of conditions being evaluated for a given expression, in this case the value of the variable languageCode
.
Let us break down the switch case code:
-
We pass an expression
to the switch
statement,
that is, within the parentheses
of switch()
. We can call this
the switch expression.
-
It is followed by a number of
cases. Each case contains
a value and a code block.
-
All switch
statements
must also contain a
default block.
-
The value of the switch expression
is then compared with the value
of each case,
to see if they are equal.
-
The code block of the first
case whose value is equal
to the value of the switch expression
is executed.
-
If the value of the switch expression
does not match with any of the cases,
then the default code block is executed.
In another example given below, we get the role of a person in a company, based on their name. When the value of name
matches with a case, the code block of that case is executed.
The above code displays This is the admin. because the value of the name
variable is equal to the value of the case Eve and the code block for that case contains console.log("This is the admin.")
followed by a break
statement.
break Statement
- The
break
statement is used to exit the execution of a switch
statement.
- If the
break
statement is not present at the end of a code block the switch
statement continues its execution to the next case block.
- As you can see in the below example, this happens irrespective of whether the condition, here the value of
name
is matched with that case or not.
If the given expression does not match with any case value, then the code block of the default case gets executed. The encouraged practice is to always write the default
block after all the cases.