Conditional Statements in TypeScript – switch

Conditional Statements are used to make decisions in the code and execute a set of statements accordingly. If a condition is true, one set of statements is executed. If the condition is false, no or another set of statements is executed.

There are two types of conditional statements: if and switch. In this article, we will discuss about switch statement.

Basic Syntax of switch statement is:

switch(variable) { 
   case constant1: { 
      //statements; 
      break; //breaks the flow of the execution
   } 
   case constant2: { 
      //statements; 
      break; 
   } 
   default: { //optional
      //statements; 
      break; 
   } 
} 
  • We test the cases against a defined value. Whichever case matches the value, that case is executed.
  • We put break statement to stop the execution of code after that case.
  • default is used when the defined value does not matches any case. This is optional.
  • There can be any number of cases in switch statements.
  • Cases should have unique constants to match the variable of Switch.
  • Data Type of Switch variable and case constant should be same.
  • Case contacts should only be constants. They should not be expressions or variable.

Below is an example when the defined value matched one of the case.

var weeknum:number = 4;
switch(weeknum){
    case 1:
        console.log("Sunday");
        break;
    case 2:
        console.log("Monday");
        break;
    case 3:
        console.log("Tuesday");
        break;
    case 4:
        console.log("Wednesday");
        break;
    case 5:
        console.log("Thursday");
        break;
    case 6:
        console.log("Friday");
        break;
    case 7:
        console.log("Saturday");
        break;
    default:
        console.log("Invalid Value");

}

Below is the output of above code.

Below is an example for default statement.

var weeknum:number = 9;
switch(weeknum){
    case 1:
        console.log("Sunday");
        break;
    case 2:
        console.log("Monday");
        break;
    case 3:
        console.log("Tuesday");
        break;
    case 4:
        console.log("Wednesday");
        break;
    case 5:
        console.log("Thursday");
        break;
    case 6:
        console.log("Friday");
        break;
    case 7:
        console.log("Saturday");
        break;
    default:
        console.log("Invalid Value");

}

Below is the output of above code:

 

Conditional Statement (switch) in JavaScript

Switch statement is just like if statement with a little modification here and there.

Below is the example of switch. You will get the result as: Front End Language! This time you will not see an alert box displaying the result, instead you will see this text written on the HTML page as we have chosen document.write() function.

var lang="C#";
switch(lang)
{
case "JavaScript":
document.write("Web Development!");
break;
case "C#":
document.write("Front End Language!");
break;
case "SQL":
document.write("Back End language!");
break;
default:
document.write("Not a great choice!");
}

In this, we put condition in switch and then that result is matched with all the subsequent cases. Whichever case value is matched first, those statements will be executed.

break; statement is used to break and come out of that case, so that any other statement won’t be executed.

default: This case is generally placed at the end of all cases and is executed when none of the above cases are matched.

Below is another example where you can put conditions in the cases.

var currentDate = new Date();
var currentHour = currentDate.getHours();
alert("Current Date: " + currentDate);
switch(true)
{
case (currentHour >= 4 && currentHour < 12):
alert("Morning!");
break;
case (currentHour >= 12 && currentHour <= 19):
alert("Afternoon!");
break;
case (currentHour > 19 && currentHour < 4):
alert("Night!");
break;
default:
alert("Invalid value!");
}
Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started