TypeScript if-else Statements

Introduction

In this chapter, we will explore if-else statements in TypeScript. These control flow statements allow you to execute different blocks of code based on certain conditions. Understanding how to use if-else statements is essential for writing logical and efficient TypeScript programs.

Table of Contents

  • If Statement
  • If-Else Statement
  • Nested If-Else Statement
  • Ternary Operator
  • Complete Example with Output
  • Conclusion

If Statement

The if statement executes a block of code if a specified condition is true.

Syntax

if (condition) {
  // code to be executed if the condition is true
}

Example

This example checks if the variable age is greater than or equal to 18 and prints a message accordingly.

let age: number = 18;

if (age >= 18) {
  console.log("You are an adult.");
}

Output

You are an adult.

If-Else Statement

The if-else statement executes one block of code if a specified condition is true and another block of code if the condition is false.

Syntax

if (condition) {
  // code to be executed if the condition is true
} else {
  // code to be executed if the condition is false
}

Example

This example checks if the variable age is greater than or equal to 18 and prints a message accordingly. If the condition is false, it prints a different message.

let age: number = 16;

if (age >= 18) {
  console.log("You are an adult.");
} else {
  console.log("You are a minor.");
}

Output

You are a minor.

Nested If-Else Statement

The nested if-else statement is an if statement inside another if or else block. It allows for multiple levels of condition checking.

Syntax

if (condition1) {
  // code to be executed if condition1 is true
  if (condition2) {
    // code to be executed if condition2 is true
  } else {
    // code to be executed if condition2 is false
  }
} else {
  // code to be executed if condition1 is false
}

Example

This example checks if age is greater than or equal to 18 and further checks if age is greater than or equal to 21.

let age: number = 20;

if (age >= 18) {
  console.log("You are an adult.");
  if (age >= 21) {
    console.log("You can drink alcohol.");
  } else {
    console.log("You cannot drink alcohol.");
  }
} else {
  console.log("You are a minor.");
}

Output

You are an adult.
You cannot drink alcohol.

Ternary Operator

The ternary operator is a shorthand for the if-else statement. It evaluates a condition and returns one value if true and another value if false.

Syntax

condition ? expressionIfTrue : expressionIfFalse

Example

This example checks if age is greater than or equal to 18 and assigns a message accordingly using the ternary operator.

let age: number = 18;
let message: string = age >= 18 ? "You are an adult." : "You are a minor.";
console.log(message);

Output

You are an adult.

Complete Example with Output

In this section, we will combine all the examples into a single TypeScript file, compile it to JavaScript, and run it to see the output.

TypeScript Code

You can test the following code in the TypeScript Playground:

// If Statement
let age: number = 18;

if (age >= 18) {
  console.log("You are an adult.");
}

// If-Else Statement
age = 16;

if (age >= 18) {
  console.log("You are an adult.");
} else {
  console.log("You are a minor.");
}

// Nested If-Else Statement
age = 20;

if (age >= 18) {
  console.log("You are an adult.");
  if (age >= 21) {
    console.log("You can drink alcohol.");
  } else {
    console.log("You cannot drink alcohol.");
  }
} else {
  console.log("You are a minor.");
}

// Ternary Operator
age = 18;
let message: string = age >= 18 ? "You are an adult." : "You are a minor.";
console.log(message);

Conclusion

In this chapter, we covered the if statement, if-else statement, nested if-else statement, and ternary operator in TypeScript. We provided a complete example with its output to illustrate how these statements work in TypeScript. Understanding if-else statements is essential for writing logical and efficient TypeScript programs.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare