EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript switch
 

TypeScript switch

Updated April 5, 2023

TypeScript switch

 

 

Introduction to TypeScript switch

The Typescript switch is one of the features, and it is a statement used to check the multiple values, and it will execute the same multiple set of statements; the switch is a single level block code corresponding to each value, and it executes the first case clause whose the values is matches if the value is not matching then the case clause is found the switch statement looks for the optional case default clause is available on the typescript switch with the help of break statement we will control the switch case statements if we did not use break keyword the code execution continues at the next statement till the switch loop terminates.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

Generally, the switch case statement used to evaluate the expressions and matches the expression values if the user input statement and the value of the variables matches the cases for the specific code blocks executed if no case expression matches the value of the variables expression, the script code is associated within the default block in a typescript switch statement.

variable Data type variable name : datatype = value;
switch(variable name)
{
case casename:{
---some logics depends upon the user requirement---
break
}
default:
{
---some logics based on the user requirement---
break;
}
}

The above code is the basic syntax for creating and utilizing the switch statements; we used the break statement to control the case executions, and it prints the datas to the next line of the output console.

How switch statement work in TypeScript?

The switch statement tested all the variables, expressions, and other methods based on user-defined and customized methods. It evaluates and validates all the statements which will be declared on the switch blocks. Basically, the switch statement has some set of rules to apply with the switch case statements. The first rule can be with any number of case statements within the switch blocks because it accepts all the data types like var, let, etc. Mainly we used var as the data type, and it will accept both numeric and non-numeric data types, and also, it will call the outside scope globally; it will calls when we used var type of variables.

In the switch statement, the second rule is it includes only the constants; it will not accept any variables and expressions from outside of the blocks. The third rule of the switch is when we used any variable with expression, i.e.) variable_expression, and the constant_expression should match with the data type for both the operations in the switch block. We can also compare the cases from one statement to another statement; the default block is optional, and the case expression should be a unique one; it can’t accept the duplicate.

Examples of TypeScript switch

Here are the following examples mention below

Example #1

Code:

var vars:string = "demo2";
var vars:string = "demo3";
var vars:string = "demo4";
var vars:string = "demo5";
var vars:string = "demo6";
switch(vars) {
case "demo": {
console.log("Welcome To My Domain Your first case");
break;
}
case "demo1": {
console.log("Welcome To My Domain Your second case");
break;
}
case "demo2": {
console.log("Welcome To My Domain Your third case");
break;
}
case "demo3": {
console.log("Welcome To My Domain Your fourth case");
break;
}
case "demo4": {
console.log("Welcome To My Domain Your fiveth case");
break;
}
case "demo5": {
console.log("Welcome To My Domain Your sixth case");
break;
}
case "demo6": {
console.log("Welcome To My Domain Your seventh case");
break;
}
case "demo7": {
console.log("Welcome To My Domain Your Eighth case");
break;
}
case "demo8": {
console.log("Welcome To My Domain Your Nineth case");
break;
}
case "demo9": {
console.log("Welcome To My Domain Your tenth case");
break;
}
case "demo10": {
console.log("Welcome To My Domain Your eleventh case");
break;
}
case "demo11": {
console.log("Welcome To My Domain Your twelth case");
break;
}
case "demo12": {
console.log("Welcome To My Domain Your thirtheen case");
break;
}
default: {
console.log("Thanks users your choice is invalid please try again");
break;
}
}

Output:

TypeScript switch output 1

In the above example, we used the switch cases at basic levels. We can also use the variables in the first statement to select the choices in the switch block. The default keyword will be the optional one of the switch statement, and it will be executed whenever the switch case is terminated or an invalid choice from the user input. We used string data type values for all the cases, and they will be initialized by using the var keyword. Suppose if the value in the variable will do not match with any of the constants, it automatically executes the default block.

Example #2

Code:

let vars = 11,
vars1 = 12;
let vars2 = 13;
switch (vars) {
case 1:console.log("Welcome");
case 2:console.log("Welcome");
case 3:console.log("Welcome");
case 4:console.log("Welcome");
case 5:console.log("Welcome");
case 6:console.log("Welcome");
case 7:{
vars2 = 14;
console.log(vars2);
break;
}
case 8:{
console.log("Welcome");
}
case 9:console.log("Welcome");
case 10:console.log("Welcome");
case 11:{
vars2 = 15;
console.log(vars2);
break;
}
case 12:
{
if (((vars % 2 == 11) &&
!(vars % 3 == 0))
|| (vars % 5 == 0))
vars2 = 16;
else
vars2 = 17;
break;
}
default:
throw Error('Thanks Users your input is invalid');
}
console.log(`${vars}  ${vars1}  ${vars2} ${vars3} ${vars4} ${vars5}`);

Output:

TypeScript switch output 2

In the second example, we used switch statement with default blocks, and in some of the cases, we have not used open and closes brackets {} and even break statement also not used it automatically executes another case after one case is the end. Here we used the if condition in one case, and it will be validated the numbers and it prints the output in the console.

Example #3

Code:

let vars= "Sivaraman";
switch (vars) {
case "Sivaraman":console.log("Welcome to my domain");
case "fjhd":console.log("jwekgf");
case "3":console.log("djkfk");
case "4":console.log("kwkjeg");
case "jhkdj":console.log("wjehgk");
case "6":console.log("Welcome");
}

Output:

output 3

In the final example, we used the same switch statements, but here we can’t use the default and break statement. So it will print all the cases statement in the console. If we used a break statement, it executes the specific case which we have already declared in the user input.

Conclusion

In typescript, we used different features for creating the web-based application in a more sophisticated. And also has some default keywords, variables, and methods for utilizing and implementing the application. We used some default conditional statement for validating the user inputs based on the customer needs, like that switch is one of the block- level statement it will be executed using the cases and default statements.

Recommended Articles

We hope that this EDUCBA information on “TypeScript switch” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. TypeScript Operators
  2. TypeScript Versions
  3. How to Install Typescript
  4. TypeScript Array

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW