E2EHIRING Logo
Jobs
Jobs
courses
Courses
mentorship
Mentorship
more
Moredropdown
E2EHIRING Logo
more
Jobs
Jobs
courses
Courses
mentorship
Mentorship
HomeSepratorIconBlogsSepratorIconJavaScript Conditional StatementsSepratorIcon

JavaScript Conditional Statements

Han SoloPriyanka Siddalingreddy
calendar28 Jun 2022
poster

JavaScript Conditional Statements


Conditional statements are used to perform different actions based on different conditions. When you write a code if you want to perform different actions for different decisions. Then you can use conditional statements in your code to do this.

In JavaScript, we have the following conditional statements.

1. if: This conditional statement is used to specify a block of code to be executed if a specified condition is true.

2. else: This conditional statement is used to specify a block of code to be executed if the same condition is false.

3. else if: This conditional statement is used to specify a new condition to test if the first condition is false.

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){
//code to be executed
}

Example

if (hour < 18){
greeting="Good Morning";
}
//Output is Good Morning

The else Statement

Use the else statement to specify a block of JavaScript code to be executed if a condition is false.

Syntax 

if(condition){
//code to be executed, if the condition is true
}else{
//code to be executed, if the condition is false
}

Example

if (hour < 18){
greeting="Good Morning";
}else{
greeting="Good Evening";
//Output is Good Morning

The else if Statement

Use the else if statement to specify a new condition to test if the first condition is false. 

Syntax 

if(condition1){
//code to be executed, if the condition1 is true
}else if(condition2){
//code to be executed, if the condition1 is false and condition2 is true
}
else{
//code to be executed, if the condition1 is false and condition2 is false 
}

Example

if (time< 10){
greeting="Good Morning";
}else if(time<20){
greeting="Good Day";
}else{
greeting="Good Evening";}

Recent Posts

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

How to publish your Android app on Google Play Store

How to publish your Android app on Google Play Store

Creating Dynamic User Interfaces with Android Motion Layout

Creating Dynamic User Interfaces with Android Motion Layout

Bean Life Cycle

Bean Life Cycle

Pom.XML

Pom.XML

copycopycopycopy

Han Solo

Recent Posts

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

How to publish your Android app on Google Play Store

How to publish your Android app on Google Play Store

Creating Dynamic User Interfaces with Android Motion Layout

Creating Dynamic User Interfaces with Android Motion Layout

Bean Life Cycle

Bean Life Cycle

Pom.XML

Pom.XML