Web Designing Tutorials

How to Write Conditional Statements in JavaScript?

Pinterest LinkedIn Tumblr
Conditional statements allow you to execute the JavaScript code only when the specified conditions match. The different types of conditional statements used in JavaScript for making different decisions. Here you will learn different ways of writing conditional statements in JavaScript.

If Statement

This statement is used to execute some code only if a specified condition is true. It was the most basic conditional statements used in JavaScript. It takes only a single condition in order to execute the statement. If the specified condition matches, it will execute the statement otherwise it won’t be executed.

Read Also: How to Write JavaScript With HTML?

Syntax:

Following is the syntax for using if statement in JavaScript.

if {condition}{
code to be executed if condition is true
}

Example:

The following JavaScript code gets the system date and extracts only the hours from it. Then if the statement checks whether the extracted value of the hour is less than 10 and executes the statement if the condition is true.

<script type="text/javascript">
var d=new Date()
var time=d.getHours()
if (time<10){
document.write("<b>Good morning</b>")
}
</script>

Preview:

This script writes “Good morning” greeting only when the time is less than 10

Read Also: How to Loop using JavaScript?

If … else statement

There is another conditional statement in JavaScript that takes two conditions. This statement is used to execute some code only if the condition is true and another code if the condition is false.

Syntax:

Following is the syntax for using if … else statement in JavaScript.

if {condition}
{
code to be executed if condition is true
}
else {
code to be executed if condition is not true
}

Example:

The following JavaScript code gets the system date and extracts only the hours from it. Then if … else statement checks whether the extracted value of the hour is less than 10. If it is less than 10, it writes “Good morning”, otherwise it writes “Good day”.

<script type="text/javascript">
var d=new Date()
var time=d.getHours()
if (time<10)
{
document.write("Good morning")
}
else {
document.write("Good Day")
}
</script>

Preview:

Following is the preview of executing the JavaScript code given above.

Read Also: How to create Changeable Date and Time Using JavaScript?

Switch statement

This statement is used to select one of many blocks of code to be executed based on the condition provided.

Syntax:

Following is the syntax for using switch statements in JavaScript.

switch(n) {
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}

Example:

The following JavaScript code gets the system date and extracts only the weekday value from it. Then the switch statement writes "Good Friday" if day value is 5, writes "Super Saturday" is the day value is 6, writes "Sleepy Sunday" if day value is 0. Lastly, the switch statement writes "I am looking forward to this weekend!" if none of the conditions match.

<script type="text/javascript">
var d=new Date();
theDay=d.getDay();
switch(theDay) {
case 5:
document.write("Good Friday");
break;
case 6:
document.write("Super Saturday");
break;
case 0:
document.write("Sleepy Sunday");
break;
default:
document.write("I am looking forward to this weekend!");
}</script>

Preview:

Following is the preview of executing the JavaScript code given above.

Read Next: How to Show Pop Up Boxes Using JavaScript?

Author

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 8 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Comments are closed.