TypeScript Switch Case


This acts like a multiple if / else if / else chain. Checks an value against a list of cases, and executes the first case that is true. If no matching case found, it executes the default case. The break(optional) statements with case indicate to the interpreter to end the particular case.

Syntax

switch (expression) {
  case case1:
    statements1
    [break;]
  case case2:
    statements2
    [break;]
  case case3:
    statements3
    [break;]
  default:
    default statements
    [break;]
}

Example1

CheckBookPrice(book:string) : string
{ 
   switch (book) {
	  case "English":
		msg = "English book price is $12.";
		break;
	  case "Math":
		msg = "Math book price is $22.";
		break;
	  case "Commerce":
		msg = "Commerce book price is $12.";
		break;
	  case "History":
		msg = "History book price is $125.";
		break;
	  case "Physics":
		msg = "Physics book price is $12.99.";
		break;
	  default:
		msg = "Sorry, book out of stock";
	}
	return msg;
}
Live Demo

If you forget a break statement, which is optional than script will run from the case where condition is met, and will run the case after that regardless if condition was met.

Example2

CheckDesignation(salary:string) : string
{ 
	switch (salary)
	{
	  case '10000': 
		msg = "Assistant";
		break;
	  case '20000': 
		msg = "Assistant Manager";
		break;
	  case '30000': 
		msg = "Relationship Manager";
		break;
	  case '40000': 
	  case '50000': 
		msg = "Manager";
		break;
	  default:  
		msg = "Unknown post";
	}
return msg;
}

Live Demo


See Also

AngularJS Built-in Filters
AngularJS Built-in Directives
AngularJS Expression
AngularJS Built-in Events
AngularJS Scope Methods
AngularJS Built-in Services
AngularJS Controller Inheritance
AngularJS Custom Directives



 
SHARE THIS IF YOU LIKE!
submit to reddit

TYPESCRIPT LANGUAGE TECHNIQUES

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.