JavaScript if else if else

 JavaScript if else if else

In JavaScript if we want to check multiple conditions together, we can use the if, else if, else statements. These are called as conditional statements.

We've already covered the if, else statements. You might want to look at that lesson before starting this one. 

These execute the code in the respective blocks depending on the conditions. The comparison might yield true or false values.

The execution of the statements are from top to bottom.

The main thing to note here is the usage of else if where multiple conditions can be checked. You can how many ever you want in there.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Doers - If else if and else statements</title>
    <p id="para1"></p>

var speedOfCar = 51
if(speedOfCar == 0 ){
  document.getElementById("para1").innerHTML = "Car is not moving";
else if(speedOfCar <= 10){
    document.getElementById("para1").innerHTML = "Car just started to move";
else if(speedOfCar <=50 && speedOfCar >10){
    document.getElementById("para1").innerHTML = "Car is moving at average speed";
  document.getElementById("para1").innerHTML = "You are speeding!";

Post a Comment