Easy Tutorial
❮ Ts Operators Home ❯

TypeScript Conditional Statements


Conditional statements are used to perform different actions based on different conditions.

TypeScript conditional statements are determined by the execution result (True or False) of one or more statements.

You can understand the execution process of conditional statements through the following diagram:


Conditional Statements

When writing code, you often need to perform different actions based on different decisions. You can use conditional statements in your code to accomplish this task.

In TypeScript, we can use the following conditional statements:


if Statement

A TypeScript if statement consists of a boolean expression followed by one or more statements.

Syntax

The syntax is as follows:

if(boolean_expression){
    // Execute if boolean_expression is true
}

If the boolean expression boolean_expression is true, the code block inside the if statement will be executed. If the boolean expression is false, the first set of code after the if statement (after the closing bracket) will be executed.

Flowchart

Example

var num:number = 5;
if (num > 0) { 
   console.log("The number is positive"); 
}

Compiling the above code results in the following JavaScript code:

var num = 5;
if (num > 0) {
    console.log("The number is positive");
}

Executing the above JavaScript code outputs:

The number is positive

if...else Statement

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax

The syntax is as follows:

if(boolean_expression){
   // Execute if boolean_expression is true
}else{
   // Execute if boolean_expression is false
}

If the boolean expression boolean_expression is true, the code inside the if block will be executed. If the boolean expression is false, the code inside the else block will be executed.

Flowchart

Example

var num:number = 12; 
if (num % 2==0) { 
    console.log("Even"); 
} else {
    console.log("Odd"); 
}

Compiling the above code results in the following JavaScript code:

var num = 12;
if (num % 2 == 0) {
    console.log("Even");
}
else {
    console.log("Odd");
}

Executing the above JavaScript code outputs:

Even

if...else if....else Statement

The if...else if....else statement is useful for executing multiple conditional checks.

Syntax

The syntax is as follows:

if(boolean_expression 1) {
    // Execute if boolean_expression 1 is true
} else if( boolean_expression 2) {
    // Execute if boolean_expression 2 is true
} else if( boolean_expression 3) {
    // Execute if boolean_expression 3 is true
} else {
    // Execute if all boolean expressions are false
}

Note the following:

Example

var num:number = 2 
if(num > 0) { 
    console.log(num+" is positive"); 
} else if(num < 0) { 
    console.log(num+" is negative"); 
} else { 
    console.log(num+" is neither positive nor negative"); 
}

Compiling the above code results in the following JavaScript code:

var num = 2;
if (num > 0) {
    console.log(num + " is positive");
}
else if (num < 0) {
    console.log(num + " is negative");
}
else {
    console.log(num + " is neither positive nor negative");
}

Executing the above JavaScript code outputs:

2 is positive

switch…case Statement

A switch statement allows testing a variable against multiple values. Each value is called a case, and the variable being tested is checked against each switch case.

The syntax for a switch statement is:

switch(expression){
    case constant-expression  :
       statement(s);
       break; // Optional
    case constant-expression  :
       statement(s);
       break; // Optional

    // You can have any number of case statements
    default : // Optional
       statement(s);
}

switch statements must follow these rules:

Flowchart

Example

var grade:string = "A"; 
switch(grade) { 
    case "A": { 
        console.log("Excellent"); 
        break; 
    } 
    case "B": { 
        console.log("Good"); 
        break; 
    } 
    case "C": {
        console.log("Pass"); 
        break;    
    } 
    case "D": { 
        console.log("Fail"); 
        break; 
    }  
    default: { 
        console.log("Invalid input"); 
        break;              
    } 
}

Compiling the above code results in the following JavaScript code:

var grade = "A";
switch (grade) {
    case "A": {
        console.log("Excellent");
        break;
    }
    case "B": {
        console.log("Good");
        break;
    }
    case "C": {
        console.log("Pass");
        break;
    }
    case "D": {
        console.log("Fail");
        break;
    }
    default: {
        console.log("Invalid input");
        break;
    }
}

Executing the above JavaScript code outputs:

Excellent
❮ Ts Operators Home ❯