Free Web Development Online Tutorials, Learn to Code
How to write if-else logic in TypeScript, TypeScript Switch Case statement Example

TypeScript If Else and Switch case statement

In this tutorial you will learn how to write conditional logic like if else logic in typescript with different operators, typescript switch case example.

How to write if else logic in TypeScript

Like all other programming language we also can write if-else conditional logic in typescript, very similar to JavaScript or C#.

So here we learn how to write conditional logic using Comparison Operators in TypeScript

if (condition) {
// execute code logic
}
let speed: number = 10;
if (speed >= 50) {
        console.log("you are at right speed")
}

TypeScript If Else example, here i am checking if speed is more than specified number.

let speed: number = 10;
if (speed >= 50) {
        console.log("you are at right speed")
}
else 
{
      console.log("you must speed up now")
}

TypeScript Switch case Statement Example

The switch statement evaluates an expression, when we have multiple conditions to check for one variable, switch is the best statement, it find matching the expression’s value to a case clause, and executes statements associated with that case.

switch(expression value) { case match1: { //statements; break; } case match2: { //statements; break; 
   } 
   default: { 
      //statements; 
      break; 
   } 
} 

Quick overview of switch case statement

  • In switch case we can include variable expression or constant which can return a value of any data type
  • We can have any number of case statements within a switch
  • We must use break keyword at the end of each case block ends with semi-colon
  • There can a optional default block
var datetime = new DateTime();
var TodayDay = datetime.Day;            
             
switch (TodayDay)
{
    case 6:
    case 0:
        console.log("Saturday Sunday, Holiday time");
        break;
    case 1:
        console.log("Monday, Have great week ahead");
        break;
    case 2:
    case 3:
    case 4:
        console.log("Working day.");
        break;
    case 5:
        console.log("Relax, Fun Friday");
     break;
}
Comparison Operators in TypeScript
Operator Comparison Operators Description
== Checks whether the values of two operands are equal or not.
let varA = 100;
let varB = 90;
console.log(varA == varB);   //false
=== Checks whether the "values and types" of two operands are equal or not.
let varA = 100;
let varB = 90;
console.log( varA === varB );  //false
console.log( varA === 10 );   //true
!= Checks whether the values of two operands are not equal
let varA = 100;
let varB = 90;
console.log( varA != varB );  //true
!== Checks whether the "values and types" of two operands are not equal
let varA = 100;
let varB = 90;
console.log( varA !== varB );  //true
> Checks whether the one variable value is greater than the second variable value
let varA = 100;
let varB = 90;
console.log( varA > varB );  //true
< Checks whether the one variable value is less than the second variable value
let varA = 100;
let varB = 90;
console.log( varA < varB );  //false
>= Checks whether the one variable value is "greater or equal" than the second variable value
let varA = 100;
let varB = 90;
console.log( varA >= varB );  //true
<= Checks whether the one variable value is "less or equal" than the second variable value
let varA = 100;
let varB = 90;
console.log( varA <= varB );  //false
Comment
Name
Email
Website
Subscribe
 
typeScript if else, switch case

Popular JavaScript Libraries

Typescript documentation

javascript course online
TypeScript comparison operators
TypeScript and JavaScript Examples