The if statement is used to run code based on a condition. You can extend it using else if and else to handle multiple conditions.
if (condition) {
// code runs if condition is true
} else if (anotherCondition) {
// code runs if anotherCondition is true
} else {
// code runs if none are true
}
let temp = 15;
if (temp > 30) {
console.log("Wear a T-shirt");
} else if (temp > 20) {
console.log("Wear a light jacket");
} else {
console.log("Wear a coat");
}
if to check conditions, else if for more options, and else for fallback code. Perfect for decision making in your scripts!
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!