The ‘if-else’ Conditional Statement in JavaScript
This is what programming is all about.
Bill Gates’s once said, “Computer programming is calculation of math and making decision with if..else”.
And it is hard to argue with that statement, and why not. Because deep down this is what programming is all about.
If you are new to this programming world, consider going through this article: 3 Things to learn while Programming for a better reference.
https://medium.com/nerd-for-tech/3-things-to-learn-while-programming-2e9b42d8b014
Anyway, if you have been in computer programming for a while, you probably know that programs are built upon logics
. And logic needs to go through certain conditions to satisfy a particular problem.
This is where conditional statements
comes into the picture. They help us to make decisions to ace a problem.
Generally, the conditional statements
are referred as if...else
. And they are pretty straightforward:
if(this happen){
do this particular task;
}else{
do this.
}
This means, if certain
condition met, do a particular task, and if the condition is not met, then do something else.
Check out this simple example:
let num = 1;if(num > 0){
console.log("The number is positive");
} else {
console.log("The number is negative");
}
Types of Conditional Statement
Typically there are two types of conditional statement exists, in any programming language:
- if…else
- else if
Let’s check out them individually:
if…else
Syntax:
if(this condition satisfies){
do a particular task
} else {
do something else.
}
This syntax is similar to what we discussed earlier. If a condition satisfies
, do a particular task, otherwise, do something else.
Example:
// Check if a number is odd or even
let num = 7;
if(num % 2 == 0){
console.log("The number is even");
} else {
console.log("the number is odd");
}
In the above example, we have checked that while dividing a number by 2, if we get 0
as remainder then the number is even
, and if we get 1
as remainder then the number is odd
.
Note: ‘%’ is the modulo operator which gives the reminder
while divide a number, whereas '/' gives the quotient
.
else if
Apart from if...else
, we have else if
, which is used to add another condition.
Syntax:
if(this condition satisfies){
do a particular task
} else if(check this condition){
do this task
} else {
do something else.
}
In this syntax, you can easily see that here is an another condition
to be checked.
We can add as many conditions as we want to be checked, with the help of else if
.
Example:
let num = 5;if(num > 0){
console.log("The number is positive");
} else if(num == 0){
console.log("The number is ZERO");
} else {
console.log("The number is negative");
}
Quite obvious; In the above program we also check whether or not is the number is 0
, and print log
statement associated with that condition
, using else..if
.
The Nested Condition
So far now, we have checked out, what a conditional statement is and its types. But we have another type of conditional statement i.e. nesting.
Nesting means checking a condition inside a particular condition.
Make sense? Check this example:
let num1 = 5;
let num2 = 8; if(num1 == num2){
console.log("Both number are equal");
} else {
if(num1 > num2){
console.log("num1 is greater than num2");
} else {
console.log("num1 is lesser than num2");
}
}
In the above example, we have a if..else
statement. But inside the else
block, we have another if..else
statement. This is called Nested Conditional Statement.
Things to Remember:
So here we have learned about the conditional statement, but there are some facts you should consider while using them.
else
always should be followed by aif
statement- use
else if
only if you need to check for another condition - If
else
done your job,else if
might not require to add
Conclusion
So let’s wrap this up. In any programming language we have two kinds of conditional statements:
- if...else
- else if
We also have a nested conditional statement, to check a condition inside a condition.
And this is all about the conditional statements in JavaScript.
This article is officially published on Within Bracket:
More on JavaScript
Thanks for sticking around. Keep Learning.
📌Find more articles here:
More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.