Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Switch | Conditional Statements
Introduction to TypeScript

SwitchSwitch

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:

In TypeScript, like in other programming languages, the switch statement is used to perform different actions based on the value of an expression. The switch statement allows you to compare the expression with multiple cases and execute the code corresponding to the first match.

The syntax for the switch-case statement looks like this:

Key points about the switch statement in TypeScript:

  • break: Typically, each case includes a break statement to exit the switch after executing the code in the corresponding case. This prevents the execution of code from other case branches. The break statement is optional, and without it, execution will continue to the next case;
  • default: default is an optional block that executes if none of the case values match the expression. It acts as an alternative for all case branches.

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:

Note

Note that if none of the values match, we execute the default block.

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?

What is the purpose of the switch statement in TypeScript?

Select the correct answer

What is the role of the default case in a switch statement?

Select the correct answer

Everything was clear?

Section 2. Chapter 6
course content

Course Content

Introduction to TypeScript

SwitchSwitch

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:

In TypeScript, like in other programming languages, the switch statement is used to perform different actions based on the value of an expression. The switch statement allows you to compare the expression with multiple cases and execute the code corresponding to the first match.

The syntax for the switch-case statement looks like this:

Key points about the switch statement in TypeScript:

  • break: Typically, each case includes a break statement to exit the switch after executing the code in the corresponding case. This prevents the execution of code from other case branches. The break statement is optional, and without it, execution will continue to the next case;
  • default: default is an optional block that executes if none of the case values match the expression. It acts as an alternative for all case branches.

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:

Note

Note that if none of the values match, we execute the default block.

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?

What is the purpose of the switch statement in TypeScript?

Select the correct answer

What is the role of the default case in a switch statement?

Select the correct answer

Everything was clear?

Section 2. Chapter 6
some-alt