Table of contents
1.
TypeScript Switch case statement
2.
Syntax
2.1.
In a switch statement, keep the following points in mind:
3.
TypeScript switch case statement Example:
3.1.
1) A normal TypeScript switch case example
3.2.
2) Grouping case example
4.
Frequently Asked Questions
4.1.
How does switch case work in TypeScript?
4.2.
Can we use string in the switch case of TypeScript?
4.3.
Can cases in switch statements have conditions?
5.
Key takeaways
Last Updated: May 30, 2024

TypeScript Switch case statement

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

TypeScript Switch case statement

The TypeScript switch statement combines numerous conditions into a single statement. It evaluates a Boolean, number, byte, short, int, long, enum type, string, and other expressions based on their value. Each value is represented by a single block of code in a switch statement. When a match is found, the block that corresponds to it is performed. The if-else-if ladder statement is similar to a switch statement.

Syntax

Given below is a general syntax for TypeScript Switch case statement:

switch ( expression ) {
   case value1:
       // statement 1
       break;
   case value2:
       // statement 2
       break;
   case valueN:
       // statement N
       break;
   default: 
       // 
       break;
}

 

The following items are included in the switch statement. A switch statement can contain any number of cases.

Case: Only one constant should be followed by a semicolon after the case. It is unable to take any further variables or expressions.

Break: To exit the switch statement after processing a case block, a break should be written at the conclusion of the block. If we don't write break, the execution will continue with the value that corresponds to the next case block.

Default: The switch statement ends with a default block. When there are no cases that will be matched, it executes.

In a switch statement, keep the following points in mind:

  • A switch statement can contain an unlimited number of cases.
  • The case values must be one-of-a-kind.
  • Constant case values are required.
  • At the end of each case statement, there is a break statement. The break statement is not required..
  • A default block is written at the conclusion of the switch statement. It's not necessary to use the default statement.

TypeScript switch case statement Example:

Let's look at some switch case statements in action.

1) A normal TypeScript switch case example

A simple switch case example that displays a message based on the target ID is shown in the following example:

let targetID = 'btnDelete';

 
switch (targetID) {
    case 'btnUpdate':
        console.log('Update');
        break;
    case 'btnDelete':
        console.log('Delete');
        break;
    case 'btnNew':
        console.log('New');
        break;
}

 

Output:

delete

 

The targetID is set to btnDelete in this example.

The targetId is compared to a list of values in the switch case statement. The 'btnDelete' statement in the associated case clause executes since the targetID matches..

2) Grouping case example

If you have a code-shared by multiple cases, you can group them. For example:

// change the month and year
let month = 2,
    year = 2020;

 
let day = 0;
switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        day = 31;
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        day = 30;
        break;
    case 2:
        // leap year
        if (((year % 4 == 0) &&
            !(year % 100 == 0))
            || (year % 400 == 0))
            day = 29;
        else
            day = 28;
        break;
    default:
        throw Error('Month Invalid);
}
console.log(`The month ${month} in ${year} has ${day} days`);

 

Output:

The month 2 in 2020 has 29 days

 

The days of a certain month and year are returned in this example.

The count of days in a month will be 31 if the numbers are 1,3, 5, 7, 8, and 12. The count of days in a month will be 30 if it is 4, 6, 9, or 11.

It yields 29 days if the month is two and the year is a leap year. Otherwise, 28 days are produced.

Frequently Asked Questions

How does switch case work in TypeScript?

The switch statement is used to check for multiple values and executes sets of statements for each of those values. We must use the break keyword at the end of each case block to stop the execution of the case block.
 

Can we use string in the switch case of TypeScript?

The TypeScript switch statement executes one statement from multiple conditions. It evaluates an expression based on its value that could be Boolean, number, byte, short, int, long, enum type, string, etc.
 

Can cases in switch statements have conditions?

A statement in the switch block can be labeled with one or more case or default labels. An if-then-else statement can test expressions based on ranges of values or conditions, whereas a switch statement tests expressions based only on a single integer, enumerated value, or String object.

Key takeaways

The TypeScript switch statement combines numerous conditions into a single statement. It evaluates a Boolean, number, byte, short, int, long, enum type, string, and other expressions based on their value.

Live masterclass