Conditional Statements in Javascript
Conditional Statements in Javascript
Javascipt
Conditional Statements :Conditional statements are used to perform different actions based on
different conditions. Very often when we write code, we want to perform different actions for different decisions.
we can use conditional statements in our code to do this.
The if Statement:
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate a JavaScript error.
Example 1:
Make a "Good day" greeting if the hour is less than 18:00:
Good day
Example2:
<html>
<body>
<script type = "javascript">
OutPut:
Qualifies for driving
Set the variable to different value and then try...
if...else statement
The 'if...else' statement is the next form of control statement that allows JavaScript to execute statements in a more
controlled way.
Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Here JavaScript expression is evaluated. If the resulting value is true, the given statement(s) in the ‘if’ block, are executed.
If the expression is false, then the given statement(s) in the else block are executed.
Example 1:
If the hour is less than 18, create a "Good day" greeting, otherwise "Good evening":
Example2:
<html>
<body>
<script type = "javascript">
</script>
2
<p>Set the variable to different value and then try...</p>
</body>
</html>
Output
Does not qualify for driving
Set the variable to different value and then try...
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
There is nothing special about this code. It is just a series of if statements, where each if is a part of the else clause of the
previous statement. Statement(s) are executed based on the true condition, if none of the conditions is true, then
the else block is executed.
Example1:
If time is less than 10:00, create a "Good morning" greeting, if not, but time is less than 20:00, create a "Good
day" greeting, otherwise a "Good evening":
Exmple 2:
<html>
<body>
<script type = "text/javascript">
3
} else {
document.write("<b>Unknown Book</b>");
}
</script>
<p>Set the variable to different value and then try...</p>
</body>
<html>
Output
Maths Book
Set the variable to different value and then try...
Switch Statement :The switch statement is used to perform different actions based
on different conditions.The objective of a switch statement is to give an expression to evaluate and several different
statements to execute based on the value of the expression. The interpreter checks each case against the value of the
expression until a match is found. If nothing matches, a default condition will be used.
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Note: The break statements indicate the end of a particular case. If they were omitted, the interpreter would
continue executing each statement in each of the following cases.
<html>
<body>
<script type = "text/javascript">
4
case 'C': document.write("Passed<br />");
break;
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
Output
Entering switch block
Good job
Exiting switch block
Set the variable to different value and then try...
Break statements play a major role in switch-case statements. Try the following code that uses switch-case statement
without any break statement.
<html>
<body>
<script type = "text/javascript">
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
Output
Entering switch block
Good job
Pretty good
Passed
Not so good
Failed
Unknown grade
Exiting switch block
Set the variable to different value and then try...