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(Ladder else if)
- Nested if statement
if Statement
Use if conditional statement if you want to execute something based on some condition.
Syntax:
if(boolean expression)
{
// code to be executed if condition is true
}
Example: if condition
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.
Example: if condition
var mySal = 1000;
var yourSal = 500;
if( mySal > yourSal)
{
alert("My Salary is greater than your salary");
}
Note:
curly braces { } is not required when if block contains only a single line to execute.
Use comparison operators carefully when writing conditional expression. For example, == and === is different.
Example: if condition
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.
Syntax:
if(condition expression)
{
//Execute this code..
}
else{
//Execute this code..
}
Example: else condition
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.
Syntax:
if(condition expression)
{
//Execute this code block
}
else if(condition expression){
//Execute this code block
}
Example: else if condition
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.
Example: Multiple if else conditions
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");
}
Points to Remember :
- 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.
JavaScript switch
The switch is a conditional statement like if statement. Switch is useful when you want to execute one of the multiple code blocks based on the return value of a specified expression.
Syntax:
switch(expression or literal value){
case 1:
//code to be executed
break;
case 2:
//code to be executed
break;
case n:
//code to be executed
break;
default:
//default code to be executed
//if none of the above case executed
}
Use break keyword to stop the execution and exit from the switch. Also, you can write multiple statements in a case without using curly braces { }.
As per the above syntax, switch statement contains an expression or literal value. An expression will return a value when evaluated. The switch can includes multiple cases where each case represents a particular value. Code under particular case will be executed when case value is equal to the return value of switch expression. If none of the cases match with switch expression value then the default case will be executed.
Example: switch Statement
var a = 3;
switch (a) {
case 1:
alert('case 1 executed');
break;
case 2:
alert("case 2 executed");
break;
case 3:
alert("case 3 executed");
break;
case 4:
alert("case 4 executed");
break;
default:
alert("default case executed");
}
In the above example, switch statement contains a literal value as expression. So, the case that matches a literal value will be executed, case 3 in the above example.
The switch statement can also include an expression. A case that matches the result of an expression will be executed.
Example: switch Statement
var a = 3;
switch (a/3) {
case 1:
alert("case 1 executed");
break;
case 2:
alert("case 2 executed");
break;
case 3:
alert("case 3 executed");
break;
case 4:
alert("case 4 executed");
break;
default:
alert("default case executed");
}
In the above example, switch statement includes an expression a/3, which will return 1 (because a = 3). So, case 1 will be executed in the above example.
The switch can also contain string type expression.
Example: switch with String Type Case
var str = "bill";
switch (str)
{
case "steve":
alert("This is Steve");
case "bill":
alert("This is Bill");
break;
case "john":
alert("This is John");
break;
default:
alert("Unknown Person");
break;
}
Multiple cases can be combined in a switch statement.
Example: Combined switch Cases
var a = 2;
switch (a) {
case 1:
case 2:
case 3:
alert("case 1, 2, 3 executed");
break;
case 4:
alert("case 4 executed");
break;
default:
alert("default case executed");
}
Points to Remember :
- The switch is a conditional statement like if statement.
- A switch statement includes literal value or is expression based
- A switch statement includes multiple cases that include code blocks to execute.
- A break keyword is used to stop the execution of case block.
- A switch case can be combined to execute same code block for multiple cases.