logo CBCE Skill INDIA

Welcome to CBCE Skill INDIA. An ISO 9001:2015 Certified Autonomous Body | Best Quality Computer and Skills Training Provider Organization. Established Under Indian Trust Act 1882, Govt. of India. Identity No. - IV-190200628, and registered under NITI Aayog Govt. of India. Identity No. - WB/2023/0344555. Also registered under Ministry of Micro, Small & Medium Enterprises - MSME (Govt. of India). Registration Number - UDYAM-WB-06-0031863

If-else condition in JavaScript!


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:

  • If the temperature is greater than 30, the first block of code will be executed, and it will log "It's a hot day!" to the console.
  • If the temperature is between 20 and 30 (inclusive), the second block of code will be executed, and it will log "The weather is pleasant." to the console.
  • If the temperature is below 20, the third block of code will be executed, and it will log "It's cold outside." to the console.

 

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.

Popular Post:

Give us your feedback!

Your email address will not be published. Required fields are marked *
0 Comments Write Comment