JavaScript supports the following types of if else statement −
- if statement
- if else statement
- if else if statement.
if statement
if statement is the statement that enables JavaScript to make decisions
and execute statements only if condition is true.
Syntax:
The syntax for a if statement is as follows –
if(expression)
{
block of code is executed if the expression is true
}
In the above syntax if the expression is true the block of code will get
executed and if expression is not true then nothing will be executed. Most
of the time comparison operators are used while making decisions.
Following is an example of if statement:
Code Implementation
<html>
<body>
<script type="text/javascript">
<!--
var student_age = 22;
if( student_age > 10 ){
document.write("<b>This is my first program </b>");
}
//-->
</script>
</body>
</html>
Note: if should be in lowercase letters. Uppercase letters (IF or If) will generate an error.
The else Statement
else statement is used to specify a block of code to be executed if the
expression is false. Following is the syntax of else statement:
Syntax
if (expression) {
block of code is executed if the expression is true
} else {
block of code is executed if the expression is false
}
In the above syntax, if the expression is true then if condition statement is
get executed, otherwise else condition is executed.
Following is an example of else statement:
Code Implementation
<html>
<body>
<script type="text/javascript">
<!--
var student_age = 5;
if( student_age > 10 ){
document.write("<b>This is my if condition. </b>");
}
else
{
document.write(“This is my else condition.”);
//-->
</script>
</body>
</html>
// output: This is my else condition.
In the above example, when the student_age is given 5 so if condition gets false as student_age should be greater than 10. So, else expression gets executed.
If else if statement
if else if statement is an advanced form of if else statement which allows
to make a correct decision out of several expression.
Following is the syntax of if else if statement:
Syntax:
if (condition 1){
code is to be executed if condition 1 is true
}
else if (condition 2){
code is to be executed if condition 2 is true
}
else if (condition 3){
code is to be executed if condition 3 is true
}
else{
code is to be executed if no condition is true
}
In above syntax, if condition 1 is not true then it goes to else if (condition 2)
statement if this statement is also false than it goes to next if else condition
the process will continue. If none of the expression is true, then
the else statement is executed.
Following is the example of if else if statement:
Code implementation
<html>
<body>
<script type="text/javascript">
<!--
var subject = "Physics";
if( subject == "english" ){
document.write("<b>This is English Subject</b>");
}
else if( subject == "Physics" ){
document.write("<b>This is physics Subject</b>");
}
else if( subject == "Chemistry" ){
document.write("<b>This is Chemistry Subject</b>");
}
else{
document.write("<b>Unknown Subject</b>");
}
//-->
</script>
<
</body>
<html>
Switch Condition
You can use multiple if...else…if statements, to perform a multiple conditions. However, this is not always the right solution, especially when all of the statements depend on the value of a single variable. Following is the syntax of switch condition:
Syntax:
switch (expression)
{
case condition n: code block
break;
case condition n: code block
break;
case condition n: code block
break;
default: code block
}
Code Implementation
<html>
<body>
<script type="text/javascript">
<!--
var name='Geeta';
switch (name)
{
case 'Neha': document.write("Neha is a good student<br />"); break;
case 'Ragini': document.write(" Ragini is good in sports.<br />"); break;
case 'Shikha': document.write("Shikha is an intelligent student<br />"); break; case 'Geeta': document.write("Geeta is a bright student<br />"); break; case 'Rohan': document.write("rohan is an average student<br />"); break; default: document.write("Unknown name<br />")
}
document.write("Exiting switch block");
//-->
</script>
</body>
</html>
// The output for the above code will be
// Geeta is a bright student.
Sushant Mishra
24-Mar-2017