Javascript-IF ELSE
The JavaScript if-else statement has executed the code whether the condition is true or false. It is a part of JavaScript’s “Conditional” Statements.
if-else conditions:
- if condition
- if-else condition
- else if condition

if condition:
Use if conditional statement if you want to execute if a specified condition is true.
Syntax:
if(condition expression)
{
// code to be executed if condition is true
}
Example:
<html>
<body>
<script>
var a=20;
if(a>10){
document.write("value of a is greater than 10");
}
</script>
</body>
</html>
OUTPUT:
value of a is greater than 10
else condition:
Use else statement when you want to execute the code every time when if the same condition evaluates to false.
Syntax:
if(condition expression)
{
//Execute this code..
}
else{
//Execute this code..
}
Example:
<html>
<body>
<script>
var a=20;
if(a%2==0){
document.write("a is even number");
}
else{
document.write("a is odd number");
}
</script>
</body>
</html>
OUTPUT:
a is even number
else if condition:
Use the “else if” condition when you want to apply the second level condition after the if statement.
Syntax:
if(condition expression)
{
//Execute this code block
}
else if(condition expression){
//Execute this code block
}
Example:
<html>
<body>
<script>
var a=20;
if(a==10){
document.write("a is equal to 10");
}
else if(a==15){
document.write("a is equal to 15");
}
else if(a==20){
document.write("a is equal to 20");
}
else{
document.write("a is not equal to 10, 15 or 20");
}
</script>
</body>
</html>
OUTPUT:
a is equal to 20