Quick Links
The JavaScript if else Statement is used to execute the code whether condition is true or false. There are three forms of if statements in javascript.
- If Statement
- If else statement
- and if else if statement
if statement
Very often when you write code, you want to perform different actions based on different conditions. You can do this by using conditional statements in your code. Use if to specify a block of code that will be executed if a specified condition is true.
if(expression){
//content to be evaluated
}
Flow chart

Let’s see the simple example:
<script>
var a=20;
if(a>10){
document.write("value of a is greater than 10");
}
</script>
If…else Statement
Use the else statement to specify a block of code that will execute if the condition is false.
if(expression){
//content to be evaluated if condition is true
}
else{
//content to be evaluated if condition is false
}
Let’s see the example of if-else statement in JavaScript to find out the even or odd number.
<script>
var a=20;
if(a%2==0){
document.write("a is even number");
}
else{
document.write("a is odd number");
}
</script>
If…else if statement
You can use the else if statement to specify a new condition if the first condition is false.
if(expression1){
//content to be evaluated if expression1 is true
}
else if(expression2){
//content to be evaluated if expression2 is true
}
else if(expression3){
//content to be evaluated if expression3 is true
}
else{
//content to be evaluated if no expression is true
}
Let’s see the simple example of if else if statement in javaScript.
<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>
Welcome to JavaScript!
JavaScript in Detail
External JavaScript
Syntax in JavaScript
Comments in JavaScript
Variables in JavaScript
Operators in JavaScript