Web Designing Tutorials Collection

3 Simple Ways to Write Conditional Statements in JavaScript

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

How to Write Conditional Statements in JavaScript

1. If Statement

This statement is used to execute some code only if a specified condition is true. It was the most basic conditional statement 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:

The following is the syntax for using an 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 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?

2. 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:

The following is the syntax for using the 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 the 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:

The following is the preview of executing the JavaScript code given above.



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

3. Switch statement

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

Syntax:

The 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 the day value is 5, writes "Super Saturday" if the day value is 6, and writes "Sleepy Sunday" if the 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:

The 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 12 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Write A Comment

Pin It

Protected by Security by CleanTalk and CleanTalk Anti-Spam