If-else condition in JavaScript
In JavaScript, you can use the if-else
statement to execute different blocks of code based on a condition. Here's the basic syntax:
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Here's an example to illustrate how the if-else
statement works:
let temperature = 25;
if (temperature > 30) {
console.log("It's a hot day!");
} else if (temperature >= 20 && temperature <= 30) {
console.log("The weather is pleasant.");
} else {
console.log("It's cold outside.");
}
In this example:
You can have multiple else if
blocks to check for additional conditions. The else
block at the end is optional and is executed if none of the previous conditions are true.
Remember to replace condition
in the if
statement with the actual condition you want to check. This condition should evaluate to either true
or false
.
Thank you.