if-else Conditional Statements in JavaScript

Words
521
Reading
3 min
Listen
Play
4y

InShot_20220909_161812576.jpg

Hello devs馃憢!!

For the past few days, I've been working on expanding my knowledge on JavaScript. It actually hasn't been as difficult as I thought it would be, as a matter of fact, it's been very interesting 馃榿. There is a whole lot to learn and it does require a lot of practice as practice helps one in mastering the syntax.

So I've been learning about conditional statements recently and today I'll be talking about the if-else conditional statement in JavaScript.

Suppose you want to perform a task or you want a block of code to run only if a particular condition is true...
For instance, you want to rest if you're tired.
You can take "rest" as your task and "if you're tired" as the condition.

This is where JavaScript conditional statements come in.

If you want a block of code to run if a particular condition is true then you can go for a loop. But if you want a different block of code to run if the condition returns false then you resort to conditional statement.

JavaScript has the following conditional statements:

  • if which is used to specify a block of code to be executed, if a specified condition returns true.

  • else which is used to specify a block of code to be executed, if the conditions specified return false.

  • else if which is used to specify a new condition to test, if the first condition specified returns false. This statement can be omitted if there is no alternate condition to test.

  • switch which is used to specify many alternative blocks of code to be executed.

But in this post, I'll only be applying the if-else statement.

Syntax:


if ( condition1 ) {
// Block of code to be executed if condition returns true
}
else if ( condition2 ) {
// Block of code to be executed if condition1 returns false and condition2 returns true
}
else {
// Block of code to be executed if both condition1 and condition2 return false
}

Example:

Here we'll be writing a code which would alert the correct greeting according to the hour of the day.

First, we start by declaring the the 'greeting' and 'hour' variables and assigning a particular value for the hour variable.

Let's assume the time is 7:00, so we'll asign the value 7 to the hour variable.

let greeting;
let hour = 7 ;

Then we can go ahead to write the if-else statement which would determine the appropriate value to be assigned to the greeting variable according to the specified hour.

let greeting;
let hour = 7 ;

if (hour < 12) {
greeting = "Good Morning" ;
}
else if (hour < 17){
greeting = "Good Afternoon" ;
}
else {
greeting = "Good Evening" ;
}

alert(greeting);// Alerts "Good Morning"

Here, condition1 returns true because 7 is less than 12 so the code alerts "Good Morning".


Now let's assume here that the time is 15:00. Then we'll assign the value 15 to the hour variable.

let greeting;
let hour = 15 ;

if (hour < 12) {
greeting = "Good Morning" ;
}
else if (hour < 17){
greeting = "Good Afternoon" ;
}
else {
greeting = "Good Evening" ;
}

alert(greeting); // Alerts "Good Afternoon"

Here, condition1 returns false, therefore condition2 is taken into consideration and in this case it returns true because 15 is less than 17, so the code will alert "Good Afternoon".


Again, we assume the time is 20:00, then we assign the value 20 to the 'hour' variable.

let greeting;
let hour = 20 ;

if (hour < 12) {
greeting = "Good Morning" ;
}
else if (hour < 17){
greeting = "Good Afternoon" ;
}
else {
greeting = "Good Evening" ;
}

alert(greeting); // Alerts "Good Evening"

In this case, both condition1 and condition2 returned false because 20 is greater than both 12 and 17. So the block of code in the else statement is run and 'Good Evening' is alerted.


That would be all for now, I really look forward to sharing more JavaScript related topics in the future.
I hope we all have a splendid weekend.

P.S. Lead image is mine.

Thanks for reading!

if-else Conditional Statements in JavaScript | Ecency