Typescript - Beginner Guide to Switch case tutorial with examples


In this blog post, we are going to learn Switch case statement with examples

Switch case Introduction

Typescript Switch case tutorial

switch case is a kind of conditional expression which checks and evaluates the expression and matched case block of code executes. This is used in place of if and else conditional expressions. It will avoid multiple if and else statements to simplify the code 

Syntax

switch(conditionalexpression)  
{  
case value_expression:  
// body statements  
break;  
case value_expression1:  
// body statements  
break;  
default:  
// body statements  
break;  
}  

conditionalexpression

conditional Expression is evaluated and matching code block is finding out using case and default 

Case Block - This code is executed when Conditional Expression is matched with case block 

Default Block - This code is executed when Conditional Expression is not matched with case block Break keyword is used to break or exit from current case block and it is optional. If it is not used, remaining cases will be executed after executing matched case statement

Example

Here Single case statement is used and the default is used if none of the cases are matched.

var value = 'a';  
switch (value) {  
  case 'a':  
    console.log(' Letter a Checked');  
    break;  
  case 'b':  
    console.log(' Letter b Checked');  
    break;  
  default:  
    console.log(' default letter Checked');  
}  

Output is

Letter a Checked  

Multiple Switch cases statement Example

Multiple cases can be placed for a single code of block. Here is an example of this

var value = 'c';  
switch (value) {  
    case 'a':  
        console.log(' Letter a Checked');  
        break;  
  case 'b':  
  case 'c':  
  case 'd':  
        console.log(' Letter b,c and d Checked');  
    break;  
  default:  
        console.log(' default letter Checked');  
    break;  
}  

Output is

Letter b,c and d Checked  

Switch Case Enum example

In switch expression, Used Enum constants. and also provided multiple case statements with enum constants

enum Day {  
  MONDAY,  
  TUESDAY,  
    WEDNESDAY,  
    THURSDAY,  
    FRIDAY,  
    SATURDAY,  
  SUNDAY  
}  

  
case Day.SATURDAY:

  
function isWorkingDay(day: Day) {  
    switch (day) {  
        case Day.SUNDAY:  
            console.log('holiday');  
            break;  
        default:  
            console.log('working day');  
            break;  
    }  
}  
const tuesday = Day.TUESDAY;  
const sunday = Day.SUNDAY;  
isWorkingDay(tuesday); // working day  
isWorkingDay(sunday); // holiday  

Output is

working day  
holiday  

Typescript Configuration - noFallthroughCasesInSwitch 

noFallthroughCasesInSwitch value is boolean ie true or false. 

This can be configured in tsconfig.json of typescript application.This is used to return an error for fall through switch cases Following is switch case fallthrough example where there are no break statements. if noFallthroughCasesInSwitch=true, The below case is not allowed

switch(value) {  
    case 1:  
        // statements  
    case 2:  
// statements 2  
}  

Similar Posts