Conditional statements are used to perform different actions based on different conditions.
Conditional statements
Often when writing code, you always need to perform different actions for different decisions. You can use conditional statements in your code to accomplish this task.
In JavaScript, we can use the following conditional statements:
If statement-use this statement to execute code only if the specified condition is true
If...else Statement-executes code when the condition is true and executes other code when the condition is false
If...else If....else Statement-Use this statement to select one of several code blocks to execute
Switch statement-Use this statement to select one of several code blocks to execute
If statement
The statement executes code only if the specified condition is true.
Grammar
if (condition)
{
Code that executes only if the condition is true
}
Note: Use lowercase if. Using uppercase letters (IF) generates JAVASCRIPT errors!
Instance
When the time is less than 20:00, a "Good day" greeting is generated:
if (time<20)
{
x= "Good Day";
}
The result of X is:
Good Day
Try it yourself.
Please note that in this syntax, there is no: else ... You have told the browser to execute the code only if the specified condition is true.
If...else statements
Use the If....else statement to execute code when the condition is true, and to execute additional code when the condition is false.
Grammar
if (condition)
{
Code to execute when the condition is true
}
Else
{
Code to execute when the condition is not true
}
Instance
When the time is less than 20:00, the greeting "Good Day" will be received, otherwise the greeting "Good evening" will be received.
if (time<20)
{
x= "Good Day";
}
Else
{
x= "Good evening";
}
The result of X is:
Good Day
Try it yourself.
If...else if...else Statements
Use the If....else if...else statement to select one of several code blocks to execute.
Grammar
if (condition 1)
{
Code executed when condition 1 is true
}
else if (condition 2)
{
Code executed when condition 2 is true
}
Else
{
Code that executes when condition 1 and condition 2 are not true
}
Instance
If the time is less than 10:00, the greeting "Good Morning" is sent, otherwise if the time is less than 20:00, the greeting "Good Day" is sent, otherwise the greeting "Good Evening" is sent:
if (time<10)
{
X= "Good Morning";
}
else if (time<20)
{
x= "Good Day";
}
Else
{
x= "Good evening";
}
The result of X is:
Good morning
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html charset=utf-8"/>
<title></title>
<body>
<p> If the time is earlier than 7:00, greet "Good Day" .</p>
<button onclick= "myFunction ()" > click here </button>
<p id = "Demo" ></p>
<script type= "Text/javascript" >
function MyFunction ()
{
var x = "";
var time = new Date (). GetHours ();
/*if (Time < 7)
{
x = "Good Day";
}
Else
{
x = "Your is a lazy guy";
}
document.getElementById ("Demo"). Innerhtml=x;
*/
if (Time < 10)
{
x = "Good Morning";
}
else if (Time < 20)
{
x = "Good Day";
}
Else
{
x = "Good evening";
}
document.write (x);
}
</script>
</body>
<meta http-equiv= "Content-type" content= "text/html charset=utf-8"/>
<title></title>
<body>
<p> If the time is earlier than 7:00, greet "Good Day" .</p>
<button onclick= "myFunction ()" > click here </button>
<p id = "Demo" ></p>
<script type= "Text/javascript" >
function MyFunction ()
{
var x = "";
var time = new Date (). GetHours ();
/*if (Time < 7)
{
x = "Good Day";
}
Else
{
x = "Your is a lazy guy";
}
document.getElementById ("Demo"). Innerhtml=x;
*/
if (Time < 10)
{
x = "Good Morning";
}
else if (Time < 20)
{
x = "Good Day";
}
Else
{
x = "Good evening";
}
document.write (x);
}
</script>
</body>
Javascript-if-else