
- TypeScript - Home
- TypeScript - Roadmap
- TypeScript - Overview
- TypeScript - Environment Setup
- TypeScript - Basic Syntax
- TypeScript vs. JavaScript
- TypeScript - Features
- TypeScript - Variables
- TypeScript - let & const
- TypeScript - Operators
- TypeScript - Types
- TypeScript - Type Annotations
- TypeScript - Type Inference
- TypeScript - Numbers
- TypeScript - Strings
- TypeScript - Boolean
- TypeScript - Arrays
- TypeScript - Tuples
- TypeScript - Enums
- TypeScript - Any
- TypeScript - Never
- TypeScript - Union
- TypeScript - Literal Types
- TypeScript - Symbols
- TypeScript - null vs. undefined
- TypeScript - Type Aliases
- TypeScript Control Flow
- TypeScript - Decision Making
- TypeScript - If Statement
- TypeScript - If Else Statement
- TypeScript - Nested If Statements
- TypeScript - Switch Statement
- TypeScript - Loops
- TypeScript - For Loop
- TypeScript - While Loop
- TypeScript - Do While Loop
- TypeScript Functions
- TypeScript - Functions
- TypeScript - Function Types
- TypeScript - Optional Parameters
- TypeScript - Default Parameters
- TypeScript - Anonymous Functions
- TypeScript - Function Constructor
- TypeScript - Rest Parameter
- TypeScript - Parameter Destructuring
- TypeScript - Arrow Functions
- TypeScript Interfaces
- TypeScript - Interfaces
- TypeScript - Extending Interfaces
- TypeScript Classes and Objects
- TypeScript - Classes
- TypeScript - Objects
- TypeScript - Access Modifiers
- TypeScript - Readonly Properties
- TypeScript - Inheritance
- TypeScript - Static Methods and Properties
- TypeScript - Abstract Classes
- TypeScript - Accessors
- TypeScript - Duck-Typing
- TypeScript Advanced Types
- TypeScript - Intersection Types
- TypeScript - Type Guards
- TypeScript - Type Assertions
- TypeScript Type Manipulation
- TypeScript - Creating Types from Types
- TypeScript - Keyof Type Operator
- TypeScript - Typeof Type Operator
- TypeScript - Indexed Access Types
- TypeScript - Conditional Types
- TypeScript - Mapped Types
- TypeScript - Template Literal Types
- TypeScript Generics
- TypeScript - Generics
- TypeScript - Generic Constraints
- TypeScript - Generic Interfaces
- TypeScript - Generic Classes
- TypeScript Miscellaneous
- TypeScript - Triple-Slash Directives
- TypeScript - Namespaces
- TypeScript - Modules
- TypeScript - Ambients
- TypeScript - Decorators
- TypeScript - Type Compatibility
- TypeScript - Date Object
- TypeScript - Iterators and Generators
- TypeScript - Mixins
- TypeScript - Utility Types
- TypeScript - Boxing and Unboxing
- TypeScript - tsconfig.json
- From JavaScript To TypeScript
- TypeScript Useful Resources
- TypeScript - Quick Guide
- TypeScript - Cheatsheet
- TypeScript - Useful Resources
- TypeScript - Discussion
TypeScript - Decision Making
In TypeScript, the decision making statements are used to control the flow of the execution based on certain conditions. TypeScript support all types of decision making constructs available in ES6, including if, if else, switch statements. As a superset of JavaScript, TypeScript inherits and expands upon JavaScripts features including decision-making statements.
Decision-making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Shown below is the general form of a typical decision-making structure found in most of the programming languages −

A decision-making construct evaluates a condition before the instructions are executed. Decision-making constructs in TypeScript are classified as follows −
S.No. | Statement & Description |
---|---|
1. |
if statement
An if statement consists of a Boolean expression followed by one or more statements. |
2. |
if...else statement
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. |
3. |
elseif and nested if statements
You can use one if or else if statement inside another if or else if statement(s). |
4. |
switch statement
A switch statement allows a variable to be tested against a list of values. |
Examples
Let's understand the decision making in detail with the help of some examples in TypeScript.
Example: If Statement
In the example below, the if statement checks the condition "age >= 18". The condition (boolean expression) is true so the statement within curly brackets ({}) is executed.
let age: number = 19; if (age >= 18) { console.log("You care eligible for voting."); }
On compiling it will generate the following JavaScript code.
let age = 19; if (age >= 18) { console.log("You care eligible for voting."); }
The output of the above example code is as follows
You are eligible for voting.
So what if the condition (boolean expression) age >= 18 is evaluated as false.
If the condition is false, else statement will be executed.
Lets check the flowing example
Example: Ifelse statement
In this example, the condition (age >= 18) is evaluated to false so the statement following the else statement is executed.
let age: number = 17; if (age >= 18) { console.log("You care eligible for voting."); } else { console.log("You are not eligible for voting.") }
On compiling, it will generate the following JavaScript code.
let age = 17; if (age >= 18) { console.log("You care eligible for voting."); } else { console.log("You are not eligible for voting.") }
The output of the above example code is as follows
You are not eligible for voting.
Example: Nested if statements
In the example below, else...if ladder used to check multiple conditions. The condition grade >= 80 evaluates to true so it will print "You got a B grade" in the console.
var grade: number = 85; if (grade >= 90) { console.log("You got an A grade"); } else if (grade >= 80) { console.log("You got a B grade "); } else if (grade >= 70) { console.log("You got a C grade "); } else if (grade >= 60) { console.log("You got a D grade "); } else { console.log("You got an F grade "); }
On compiling, it will generate the following JavaScript code.
var grade = 85; if (grade >= 90) { console.log("You got an A grade"); } else if (grade >= 80) { console.log("You got a B grade "); } else if (grade >= 70) { console.log("You got a C grade "); } else if (grade >= 60) { console.log("You got a D grade "); } else { console.log("You got an F grade "); }
The output of the above example code is as follows
You got a B grade
Example: Switch statement
The example below, we use the grade variable as an expression of switch case statement. It verifies the value of grade against the case constants (A, B and C) and executes the corresponding block. If value of grade does not match with any case value, the default case will be executed.
var grade: string = 'B'; switch (grade) { case 'A': { console.log("Excellent"); break; } case 'B': { console.log("Good"); break; } case 'C': { console.log("Fair"); break; } default: console.log("Unknown grade"); }
On compiling, it will generate the following JavaScript code.
var grade = 'B'; switch (grade) { case 'A': { console.log("Excellent"); break; } case 'B': { console.log("Good"); break; } case 'C': { console.log("Fair"); break; } default: console.log("Unknown grade"); }
The output of the above example code is as follows
Good