In writing a program, there may be a situation when you need to take a path out of a given two paths. Therefore, you need to use conditional statements to let programs make the right decisions and perform the right actions.
JavaScript supports its use to perform operating condition statements that differ according to different conditions. Here, we will explain if. Else statement.
JavaScript supports IF.. The Else statement forms the following:
- If statement
- If...else statement
- If...else If ... Statement.
If statement:
An If statement is a basic control statement that allows JavaScript to make decisions and conditionally execute statements.
Grammar:
if (expression) {
Statement (s) to was executed if expression is true
}
The JavaScript expression here expression evaluated. If the resulting value is true, the given statement executes. If the expression is false, the declaration is not executed. Most of the time you will use comparison operations when using decisions.
Example:
<script type= "Text/javascript" >
<!--
var age =;
if (age >) {
document.write (' <b>qualifies for driving</b> ');
}
-->
</script>
This will produce the following results:
If...else statement:
The IF ... else statement is the next form of the control statement, allowing JavaScript to execute more controllable statements.
Grammar
if (expression) {
Statement (s) to be executed if expression am true
}else{
Statement (s) to be executed if Expre Ssion is False
}
Here JavaScript expression is evaluated. If the result value is true, the given statement is executed in the If Block (S). If the expression is false, the set ELSE statement block is executed.
Example:
<script type= "Text/javascript" >
<!--
var age =;
if (age >) {
document.write (' <b>qualifies for driving</b> ');
} else{
document.write ("<b>does not qualify for driving</b>");
}
-->
</script>
This will produce the following results:
Does not qualify for driving
If...else If ... Syntax:
In the If...else if .... The control statement of a level-pushing form of JavaScript makes the right decisions out of several conditions.
Grammar
if (expression 1) {
Statement (s) to am executed if expression 1 is true
}else if (expression 2) {
Statement (s) t o be executed if expression 2 am true
}else if (expression 3) {
Statement (s) to being executed if expression 3 is true
}else{
Statement (s) to was executed if no expression is true
}
There is nothing special about the code. This is just a series of if statements, where statements preceding each if are part of the ELSE clause. The declaration is executed based on a true condition, and if the condition is true, the else block executes.
Example:
<script type= "Text/javascript" >
<!--
var book = "Maths";
if (book = = "History") {
document.write ("<b>history book</b>");
} else if (book = = "maths") {
document.write ("<b>maths book</b>");
} else if (book = = "Economics") {
document.write ("<b>economics book</b>");
} else{
document.write ("<b>unknown book</b>");
}
-->
</script>
This will produce the following results: