The Typescript switch statement (or switch case statement) evaluates a given expression. It then matches the result of that expression with the values in the case clause. If it finds a match, then it executes the statements associated with that matching case
clause. It also executes the statements in case
‘s that follow the matching case
. You can break out of a switch using the break statement or using the return statement
Table of Contents
Switch Case Syntax
The Syntax of the Switch Statement in Typescript is as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | switch (expression) { case value1: //Statements executed when the //result of expression matches value1 [break;] case value2: //Statements executed when the //result of expression matches value2 [break;] ... case valueN: //Statements executed when the //result of expression matches valueN [break;] [default: //Statements executed when none of //the values match the value of the expression [break;]] } |
The switch statement starts with keyword switch and expression in parentheses. The Typescript evaluates the expression
(switch expression) and compares it with values in the case clause.
We follow switch expression with curly braces with one or more case clauses and an optional default clause.
Each case clause must have a value terminated by a colon. The default clause must not have a value. Within the individual case clause, we may write several statements
How it works
First, the switch expression evaluates its expression.
It then starts to compare the result of the expression with each case value.
When it finds a match, the switch statement then executes the statements starting from the code associated with the case clause that matches. It continues to execute the statements until it reaches
- end of the switch or
- it encounters a break statement or
- it encounters a return statement
Only the first match is considered, even if there is more than one match.
If none of the case values matches with the expression result, then it will start to execute the statements starting from the code associated with the default clause.
By convention, the default
clause is the last clause, but it does not need to be so.
Switch Examples
Using Switch
In the following example, the expression operation
matches the case clause -
. Hence the switch executes the code associated with that case clause. When it encounters the break
, it breaks out of the switch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | let val1 = 20; let val2 = 10; let operation = "-"; console.log("switch example"); switch (operation) { case "+": console.log(val1 + val2); break; case "-": console.log(val1 - val2); break; case "*": console.log(val1 * val2); break; case "/": console.log(val1 / val2); break; default: console.log("Invalid operator"); } console.log("switch finished"); ***Console *** switch example 20 switch finished |
Without break
If we omit the break
the execution of the statements continues till it reaches the end of the switch.
In the following example, we remove all the break statements. Here the execution starts from the -
operator but does not end in that case clause. The execution continues to *
,/
& even to the default clause.
The execution of statements continues until it finds one of the following
- end of the switch
- break statement
- return statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | let val1 = 20; let val2 = 10; let operation = "-"; console.log("switch example"); switch (operation) { case "+": console.log(val1 + val2); case "-": console.log(val1 - val2); case "*": console.log(val1 * val2); case "/": console.log(val1 / val2); default: console.log("Invalid operator"); } console.log("switch finished"); *** Console *** switch example 10 200 2 Invalid operator switch finished |
Default Clause
In this example, the expression does not match any case clause. Hence the switch executes the default clause.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | let val1 = 20; let val2 = 10; let operation = "="; console.log("switch example"); switch (operation) { case "+": console.log(val1 + val2); case "-": console.log(val1 - val2); case "*": console.log(val1 * val2); case "/": console.log(val1 / val2); default: console.log("Invalid operator"); } console.log("switch finished"); *** Console *** switch example Invalid operator switch finished |
Default Clause Location
Ideally and for better readability, the default
clause must be placed last. Nonetheless, even if you position it anywhere in the switch it will still work.
In this example. we place the default clause at the top and the result is the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | let val1 = 20; let val2 = 10; let operation = "="; console.log("switch example"); switch (operation) { default: console.log("Invalid operator"); break; case "+": console.log(val1 + val2); break; case "-": console.log(val1 - val2); break; case "*": console.log(val1 * val2); break; case "/": console.log(val1 / val2); break; } console.log("switch finished"); *** Console *** switch example Invalid operator switch finished |
Return also breaks from a switch
In the following example, we move the above function inside a calculator function and use the return statement return to break the switch and return from it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | let val: number = calculator(20, 10, "+"); console.log(val); function calculator(val1: number, val2: number, operation: string) { switch (operation) { default: console.log("Invalid operator"); return 0; case "+": return val1 + val2; case "-": return val1 - val2; case "*": return val1 * val2; break; case "/": return val1 / val2; } } |
Please note that multiple return statements like the one above make It harder to understand especially if it is a very lengthy routine.
Grouping of Cases
The switch case gives us the unique ability to group multiple conditions together by omitting the break clause.
In the following example, values 1 to 5 execute the same code and so is 6 to 10.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | let options = "3"; switch (options) { case "1": case "2": case "3": case "4": case "5": console.log("<=5"); break; case "6": case "7": case "8": case "9": case "10": console.log(">5 & <=10"); break; default: console.log("Invalid"); } |
Strict Equality Check
The Typescript has two operators for checking equality. One is ==
(equality operator or loose equality operator) and the other one is ===
(strict equality operator).
The Switch uses the Strict Equality Check while checking for values. If the types are different, then it returns false.
The following example returns Invalid
, because the 3
(number) is not equal to "3"
(string).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | let options: any = 3; switch (options) { case "1": console.log("1"); break; case "2": console.log("2"); break; case "3": console.log("3"); break; case "4": console.log("4"); break; default: console.log("Invalid"); } |
Also, note the use of any data type. Otherwise, it will result in a compiler error Type 'string' is not comparable to type 'number'.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | let options: any = 3; switch (options) { case 1: console.log("1"); break; case 2: console.log("2"); break; case 3: console.log("3"); break; case 4: console.log("4"); break; default: console.log("Invalid"); } |
Expressions in Value
You can also make use of expressions in value as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | let options = 3; let a = 1; let b = 2; switch (options) { case a: console.log("1"); break; case a + 1: console.log("2"); break; case b + 1: console.log("3"); break; case b * b: console.log("4"); break; default: console.log("Invalid"); } |
Typescript switch case with enum
The following example shows how to use the Typescript enum with the Switch case statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | enum VehcileType { Car, //0 Plane, //1 Train //2 } let num:VehcileType=VehcileType.Plane switch (+num) { case VehcileType.Car: console.log("Car"); break; case VehcileType.Plane: console.log("Plane"); break; case VehcileType.Train: console.log("Train"); break; default: break; } |
Note the use of +num in the above example. +
coerces the num
to a number. Otherwise the Typecript compiler throws an error Type 'VehcileType.Car' is not comparable to type 'VehcileType.Plane'
. This is a bug in Typescript.