Switch
If you're already tired of the if-else statement, I have some great news for you! In TypeScript, there's another construct for checking multiple conditions - the switch-case statement. This construct was created to execute code based on the value that's being passed. Let's take a look at the definition:
The syntax for the switch-case statement looks like this:
switch (expression) {
case value1:
// Code to execute if expression equals value1
break; // Optional break statement to exit the switch
case value2:
// Code to execute if expression equals value2
break;
// Additional cases...
default:
// Code to execute if none of the cases match the expression
}
Key points about the switch statement in TypeScript:
-
break: Typically, eachcaseincludes abreakstatement to exit theswitchafter executing the code in the correspondingcase. This prevents the execution of code from othercasebranches. Thebreakstatement is optional, and without it, execution will continue to the nextcase; -
default:defaultis an optional block that executes if none of thecasevalues match the expression. It acts as an alternative for allcasebranches.
The course author is running out of imagination, so let's look at the example with the days of the week again. However, this time we will slightly change the conditions, and now we will determine the name of the day of the week by its number in the week:
123456789101112131415161718let day: number = 3; let dayName: string; switch (day) { case 1: dayName = "Monday"; break; case 2: dayName = "Tuesday"; break; case 3: dayName = "Wednesday"; break; default: dayName = "Unknown"; } console.log(`Today is ${dayName}`);
Note
Note that if none of the values match, we execute the
defaultblock.
We use the variable day as an expression, and depending on its value, we determine the name of the day of the week. This way, we can create multiple conditions and execute specific code based on them.
1. What is the purpose of the switch statement in TypeScript?
2. What is the role of the default case in a switch statement?
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Awesome!
Completion rate improved to 2.94
Switch
Свайпніть щоб показати меню
If you're already tired of the if-else statement, I have some great news for you! In TypeScript, there's another construct for checking multiple conditions - the switch-case statement. This construct was created to execute code based on the value that's being passed. Let's take a look at the definition:
The syntax for the switch-case statement looks like this:
switch (expression) {
case value1:
// Code to execute if expression equals value1
break; // Optional break statement to exit the switch
case value2:
// Code to execute if expression equals value2
break;
// Additional cases...
default:
// Code to execute if none of the cases match the expression
}
Key points about the switch statement in TypeScript:
-
break: Typically, eachcaseincludes abreakstatement to exit theswitchafter executing the code in the correspondingcase. This prevents the execution of code from othercasebranches. Thebreakstatement is optional, and without it, execution will continue to the nextcase; -
default:defaultis an optional block that executes if none of thecasevalues match the expression. It acts as an alternative for allcasebranches.
The course author is running out of imagination, so let's look at the example with the days of the week again. However, this time we will slightly change the conditions, and now we will determine the name of the day of the week by its number in the week:
123456789101112131415161718let day: number = 3; let dayName: string; switch (day) { case 1: dayName = "Monday"; break; case 2: dayName = "Tuesday"; break; case 3: dayName = "Wednesday"; break; default: dayName = "Unknown"; } console.log(`Today is ${dayName}`);
Note
Note that if none of the values match, we execute the
defaultblock.
We use the variable day as an expression, and depending on its value, we determine the name of the day of the week. This way, we can create multiple conditions and execute specific code based on them.
1. What is the purpose of the switch statement in TypeScript?
2. What is the role of the default case in a switch statement?
Дякуємо за ваш відгук!