JavaScript if...else Statements
JavaScript includes if/else conditional statements to control the program flow, similar to other programming languages.
JavaScript includes following forms of if-else statements:
- if Statement
- if else Statement
- else if Statement
if Statement
Use if conditional statement if you want to execute something based on some condition.
if(boolean expression) { // code to be executed if condition is true }
if( 1 > 0)
{
alert("1 is greater than 0");
}
if( 1 < 0)
{
alert("1 is less than 0");
}
In the above example, the first if statement contains 1 > 0 as conditional expression. The conditional expression 1 > 0 will be evaluated to true, so an alert message "1 is greater than 0" will be displayed, whereas conditional expression in second if statement will be evaluated to false, so "1 is less than 0" alert message will not be displayed.
In the same way, you can use variables in a conditional expression.
var mySal = 1000;
var yourSal = 500;
if( mySal > yourSal)
{
alert("My Salary is greater than your salary");
}
Use comparison operators carefully when writing conditional expression. For example, == and === is different.
if(1=="1")
{
alert("== operator does not consider types of operands");
}
if(1==="1")
{
alert("=== operator considers types of operands");
}
else condition
Use else statement when you want to execute the code every time when if condition evaluates to false.
The else statement must follow if or else if statement. Multiple else block is NOT allowed.
if(condition expression) { //Execute this code.. } else{ //Execute this code.. }
var mySal = 500;
var yourSal = 1000;
if( mySal > yourSal)
{
alert("My Salary is greater than your salary");
}
else
{
alert("My Salary is less than or equal to your salary");
}
else if condition
Use "else if" condition when you want to apply second level condition after if statement.
if(condition expression) { //Execute this code block } else if(condition expression){ //Execute this code block }
var mySal = 500;
var yourSal = 1000;
if( mySal > yourSal)
{
alert("My Salary is greater than your salary");
}
else if(mySal < yourSal)
{
alert("My Salary is less than your salary");
}
JavaScript allows multiple else if statements also.
var mySal = 500;
var yourSal = 1000;
if( mySal > yourSal)
{
alert("My Salary is greater than your salary");
}
else if(mySal < yourSal)
{
alert("My Salary is less than your salary");
}
else if(mySal == yourSal)
{
alert("My Salary is equal to your salary");
}
We will learn about switch case in the next section.
- Use if-else conditional statements to control the program flow.
- JavaScript includes three forms of if condition: if condition, if else condition and else if condition.
- The if condition must have conditional expression in brackets () followed by single statement or code block wrapped with { }.
- 'else if' statement must be placed after if condition. It can be used multiple times.
- 'else' condition must be placed only once at the end. It must come after if or else if statement.