TypeScript switch statement

TypeScript switch statement is used to execute a block of statement based on the switch expression value. It is like if else if statement.

Syntax:

switch(expression){ 
case value1: 
//TypeScript block of statements 
break;   
case value2: 
//TypeScript block of statements 
break;   
...   
default: 
//TypeScript block of statements 
break;   
} 

TypeScript Switch Statement Example:

var today:string;
switch (new Date().getDay()) {
    case 0:
        today = "Sunday";
        break;
    case 1:
        today = "Monday";
        break;
    case 2:
        today = "Tuesday";
        break;
    case 3:
        today = "Wednesday";
        break;
    case 4:
        today = "Thursday";
        break;
    case 5:
        today = "Friday";
        break;
    case  6:
        today = "Saturday";
} 
console.log("Today is "+ today);  

 

Please follow and like us:
Content Protection by DMCA.com