Else If in JavaScript

The Else If in JavaScript is instrumental when we have to check several conditions. We can also use Nested If to achieve the same. Still, as the number of conditions increases, code complexity will also increase. The syntax of Else If in Javascript is as follows:

if (condition1)
       statements 1
else if (condition2)
       statements 2
else if (condition3)
       statements 3
      ...........
else if (conditionn)
       statements n
else
      default statements

The JavaScript Else If statement handles multiple expressions effectively by executing them sequentially. It will check for the first condition. If the expression evaluated to TRUE, then it will execute the lines of code present in that block. If the expression evaluated to FALSE, then it will check the Next one (Else If condition) and so on.

There will be some situations where condition1, condition2 is TRUE, for example: x = 20, y = 10

Condition1: x > y //TRUE

Condition2: x != y //TRUE

In these situations, code under Condition1 will execute. Because ELSE IF conditions will only execute if it’s previous IF or ELSE IF statement fails.

Else If in JavaScript Example

In this program, We are going to calculate whether he is eligible for scholarship or not using JavaScript Else if statement

<!DOCTYPE html>
<html>
<head>
    <title>ElseIfinJavaScript</title>
</head>
 <h1>ElseIfinJavaScript</h1>
<body>
<script>
    var Totalmarks = 380;
    if (Totalmarks >= 540)
    {
        document.write("<b> Congratulations </b>"); 
        document.write("<br\> You are eligible for Full Scholarship " ); 
    }
    else if(Totalmarks >= 480)
    {
        document.write("<b> Congratulations </b>"); 
        document.write("<br\> You are eligible for 50 Percent Scholarship " ); 
    }
    else if (Totalmarks >= 400)
    {
        document.write("<b> Congratulations </b>"); 
        document.write("<br\> You are eligible for 10 Percent Scholarship " ); 
    }
    else
    {
        document.write("<b> You are Not eligible for Scholarship </b>");
        document.write("<br\> We are really Sorry for You" );    
    }
</script>
</body>
</html>

OUTPUT 1: In this JS example, the Totalmarks= 550. First If condition is TRUE that’s why the code inside the If Statement displayed as Browser output

Else If in JavaScript 1

OUTPUT 2: To demonstrate the JavaScript Else if statement, we are going to change the Totalmarks to 500 means first IF condition is FALSE. It will check the (Totalmarks>= 480), which is TRUE, so that it will display the lines inside this block. Although (Total marks>= 400) condition is TRUE, but it won’t check that expression.

Congratulations
You are eligible for 50 Percent Scholarship

Else If in JavaScript output 3: We are going to change Totalmarks to 440 means first IF condition, (Total marks= 480) are FALSE. So, It will check the else if(Totalmarks>= 400), which is TRUE, so that it will print the code inside this block.

Congratulations
You are eligible for 10 Percent Scholarship

OUTPUT 4: We are going to change Totalmarks to 380 means all the IF expression Fail. So, It will print the code inside the else block.

You are Not eligible for Scholarship
We are really Sorry for You