TypeScript Switch Statement

The switch statement in TypeScript allows for the execution of a single statement from a set of conditions. It assesses an expression depending on its value, which may be of types such as Boolean, number, byte, short, int, long, enum type, string, and others. Each value within a switch statement is linked to a specific block of code. Once a match is identified, the associated block is executed. The functionality of a switch statement is similar to that of an if-else-if chain.

The following points must be remembered in a switch statement:

  • There can be N number of cases inside a switch statement.
  • The case values must be unique.
  • The case values must be constant.
  • Each case statement has a break statement at the end of the code. The break statement is optional.
  • The switch statement has a default block which is written at the end. The default statement is optional.
  • Syntax

    Example
    
    switch(expression){
    
    case expression1:
        //code to be executed;
        break;  //optional
    
    case expression2:
        //code to be executed;
        break;  //optional
        ........
        
    default:
        //when no case is matched, this block will be executed;
        break;  //optional
    }
    

The switch statement encompasses the following elements. It can include multiple cases within a switch statement.

Case: The case must be succeeded by a single constant and a semicolon. It is not permissible to include an additional variable or expression.

Break: The break statement must be placed at the conclusion of a case block to exit the switch statement after the case has been executed. Omitting the break will result in the execution flow continuing into the next case block with a matching value.

Default: The default section must be positioned at the conclusion of the switch statement. It runs when none of the cases match.

Example

Example

let a = 3;
let b = 2;

switch (a+b){
    case 1: {
        console.log("a+b is 1.");
        break;
    }
    case 2: {
        console.log("a+b is 5.");
        break;
    }
    case 3: {
        console.log("a+b is 6.");
        break;
    }
    
    default: {
        console.log("a+b is 5.");
        break;
    }
}

Output:

Switch case with String

Example

let grade: string = "A";
switch (grade)
{ 
    case'A+':
      console.log("Marks >= 90"+"\n"+"Excellent");
      break;

    case'A':
      console.log("Marks [ >= 80 and <90 ]"+"\n"+"Good");
      break;

    case'B+':
      console.log("Marks [ >= 70 and <80 ]"+"\n"+"Above Average");
      break;

    case'B':
      console.log("Marks [ >= 60 and <70 ]"+"\n"+"Average");
      break;

    case'C':
      console.log("Marks < 60"+"\n"+"Below Average");
      break;

    default:
        console.log("Invalid Grade.");
}

In this instance, we possess a string variable named grade. The switch statement assesses the value of the grade variable, compares it with the case clauses, and subsequently executes the corresponding statements linked to the matched case.

Output:

Switch Case with Enum

In TypeScript, the switch statement can be utilized alongside Enum in several manners.

Example

Example

enum Direction {
    East,
	West,
	North,
    South    
};
var dir: Direction = Direction.North;

function getDirection() {
    switch (dir) {
        case Direction.North: console.log('You are in North Direction');
            break;
        case Direction.East: console.log('You are in East Direction');
            break;
        case Direction.South: console.log('You are in South Direction');
            break;
        case Direction.West: console.log('You are in West Direction');
            break;
    }
}
getDirection();

Output:

TypeScript Switch Statement is fall-through.

The switch statement in TypeScript exhibits fall-through behavior. This indicates that, in the absence of a break statement, all subsequent statements following the first matching case will be executed.

Example

Example

let number = 20;  
switch(number)
{  
    //switch cases without break statements  
    case 10: console.log("10");  
    case 20: console.log("20");  
    case 30: console.log("30");  
    default: console.log("Not in 10, 20 or 30");
}

Output:

Input Required

This code uses input(). Please provide values below: