Introduction:

In this blog we can see how to use switch() statement in TypeScript .

A switch statement is a useful tool for expressing a certain kind of condition. If just one expression is evaluated for equality with a variety of values, it can be used in place of an if statement with numerous else. A control structure known as the case structure is implemented by the switch statement.

Switch is the first word in the switch statement, followed by a switch expression enclosed in brackets. This phrase is not reasonable in any way. It is a single-returning expression that will be used to choose which case to apply to. Frequently, the expression consists of just one variable.

Once the expression’s value has been determined, the switch statement then verifies each of the case label values. After the first case label that equals the expression’s outcome is reached, the code that follows it starts to run. Depending on whatever comes first, the switch statement’s end or a break statement, it continues to run.

The switch statement begins running the code that follows the default label if no case labels match the value in the switch expression. But, this standard situation is not required.

Syntax : 

Example :

class switchcase {  
    employeeName() {  
     var employee: string;  
     employee = (“Enter a name”);  
     switch (employee) {  
      case ‘crmonce’ :
       alert(“Abcd”);  
       break;  
      case ‘infosys’:  
       alert(“Xyz”);  
       break;  
      case ‘wipro’:  
       alert(“Kcdc”);  
       break;  
      case ‘jagans’:  
       alert(“mpc”);  
       break;  
      case ‘kcjc’:  
       alert(“bipc”);  
       break;  
      default:  
       alert(“Not matching….”);  
     }  
    }  
   }  
   window.onload = () => {  
    var greeter = new switchcase();  
    greeter.employeeName();  
   };  

For any Help or Queries Contact us on info@crmonce.com or +918096556344