In this tutorial, we will learn about else-if statements in TypeScript. We will cover the basics of conditional execution using if-else-if statements.
An else-if statement is a conditional statement that allows multiple conditions to be tested sequentially. It provides a way to execute different code blocks based on different conditions.
The syntax for the else-if statement in TypeScript is:
if (condition1) {
// Code block to execute if condition1 is true
} else if (condition2) {
// Code block to execute if condition2 is true
} else {
// Code block to execute if none of the conditions are true
}
The else-if statement evaluates the specified conditions in order. The first condition that is true will have its code block executed; if none of the conditions are true, the code block inside the else statement is executed.
num
.num
.num
is positive, negative, or zero.num
is positive, negative, or zero.let num: number = -5;
if (num > 0) {
console.log(`${num} is positive.`);
} else if (num < 0) {
console.log(`${num} is negative.`);
} else {
console.log(`${num} is zero.`);
}
-5 is negative.
marks
.marks
.marks
.let marks: number = 85;
if (marks >= 90) {
console.log('Grade: A');
} else if (marks >= 80) {
console.log('Grade: B');
} else if (marks >= 70) {
console.log('Grade: C');
} else if (marks >= 60) {
console.log('Grade: D');
} else {
console.log('Grade: F');
}
Grade: B
temperature
.temperature
.temperature
.let temperature: number = 75.5;
if (temperature > 100) {
console.log('It's extremely hot.');
} else if (temperature > 85) {
console.log('It's hot.');
} else if (temperature > 60) {
console.log('It's warm.');
} else if (temperature > 32) {
console.log('It's cold.');
} else {
console.log('It's freezing.');
}
It's warm.