Loading, please wait...

If…Else Statement

The If…Else Statement

The if else statement mainly specifies two possibilities. The “if” block of code executes if the specified condition is true. Otherwise, it executes the else block.

 

Syntax

If (condition)
{
     Block of code to be executed if the specified condition is true
} else {
    
     Block of code to be executed if the specified condition is false
}

 

Code:

<html>
<body>
<h2>The If Else Statement</h2>
<script>
var a = 5;
if(a>20){
document.write("Value of a is greater than 10");
} else {
document.write("Value of a is lesser than 10");
}
</script>
</body>
</html>

Here, in this code as the value of “a” is lesser than 20. So, the condition in the “if” statement is false then the program executes the “else” block of code rather than the “if” block of code.

 

Output: