Else... If Statement
The Else If Statement
The Else if statement is a new version of the If Else statements, it specify different outputs based on different conditions.
This statement specifies a new condition if the first condition is false.
Syntax
if (condition 1)
{
Code get executed if condition 1 is true
} else if (condition 2)
{
Code get executed if the condition 2 is true
} else
{
Code get executed if no condition is true
}
Code:
<html>
<body>
<h2>The Else If Statement</h2>
<script>
var a = 20;
if(a==5){
document.write("Value of a is equal to 5");
} else if (a==10) {
document.write("Value of a is equal to 10");
}else{
document.write("Value of a is equal to 20");
}
</script>
</body>
</html>
According to the code, the value of “a” is 20. Further, there are different blocks of codes according to the different set of statements as the conditions in “if” and “else if” statements are false.
So, the program hand over the execution process towards the “else” block of code and the code get executed.
Output: