
If else if statement in Java Script with Examples
Updated: Feb 16
If Statement: If statement is execute when condition is true. If condition is not satisfied then statement is not execute. It means we get result when condition is satisfied and condition is not satisfy then result is not show.
Syntax:
If (Condition)
statement;
Program:
<html>
<body>
<h2>Enter Age</h2>
<input id="age"><br><br>
<button onclick="myfun()">check my age </button>
<br><br>
<script type="text/javascript">
function myfun()
{
var age=document.getElementById('age').value;
if(age>18)
{
document.write("Ealder");
}
}
</script>
</body>
</html>
If and Else statement :
If and else statement is also conditional statement. In this statement when condition is true then program is execute and condition is not satisfied then statement is execute. It means we get result when condition is satisfied and condition is not satisfy then result also show.
Syntax:
If (Condition)
statement;
else
statement;
Program:
<html>
<body>
<h2>Enter Age</h2>
<input id="age"><br><br>
<button onclick="myfun()">check my age </button>
<br><br>
<script type="text/javascript">
function myfun()
{
var age=document.getElementById('age').value;
if(age>18)
{
document.write("Ealder");
}
else
{
document.write("child");
}
}
</script>
</body>
</html>
If else if statement in Java Script; We use this statement when we need more than two condition.
Syntax:
If (Condition)
statement;
else if (condition)
statement;
else
statement;
Program:
<html>
<body>
<h2>Enter Age</h2>
<input id="age"><br><br>
<button onclick="myfun()">check my age </button>
<br><br>
<script type="text/javascript">
function myfun()
{
var age=document.getElementById('age').value;
if(age>18)
{
document.write("Ealder");
}
else if(age==18)
{
document.write("Younger");
}
else
{
document.write("child");
}
}
</script>
</body>
</html>