if-else Conditional Statements in JavaScript
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!
That's because you're still scratching the surface 😆 or maybe because you know another programming language (that's the case for me).
I started learning JS a few days ago but I quickly brushed through all these parts about if statements, while loops and all other basic JS concepts because they are mostly the same with C, so I went straight to dom manipulation.
You have a solid knowledge of the if statement and I love the way you explained it. I would love to see another article like this but using the switch statement and possibly the ternary operator. Keep it up bro 👍
I was actually hoping to hear this😂.But it's the truth anyway, I'm still going through the basics and I'm very well aware that there's a whole lot of brain racking awaiting me😅.
JavaScript is actually the first programming language I'm learning but it's funny how I got to learn some dom manipulation and even some jQuery before actually setting for the basics 😂 , I guess I've decided to come clean now 😁.
I wanted to add the switch and tenary operator but I just thought I should keep the post short, maybe that would be another time.
Anyways, thanks a lot for the kind words bro, I really appreciate 💪.
I am currently taking Js with freecodecamp and at first I felt it wasn't difficult but by the time they started giving me complicated problems to solve, I choked 😆. You're doing quite well bro, good luck to you on your Js journey
Thanks bro💪, and you too
Nice! I think I have written a similar article on JavaScript but can't remember what it was on exactly. But you have shown good understanding on these conditional statement.
Thanks a lot😁
Congratulations @charlrific! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):
Your next target is to reach 600 upvotes.
You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Check out the last post from @hivebuzz:
Support the HiveBuzz project. Vote for our proposal!
Thanks buzz💪
You're welcome @charlrific.
BTW, we need your help!
May we ask you to support our proposal so our team can continue its work?
You can do it on Peakd, ecency, Hive.blog or using HiveSigner
https://peakd.com/me/proposals/199
Your support would be very helpful.
Thank you!