DEV Community

Cover image for if else in JavaScript - Conditional Statements
Sarvesh Prajapati
Sarvesh Prajapati

Posted on

if else in JavaScript - Conditional Statements

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 all about.

If you are new to this programming world, consider going through this article for a better reference.:
đź“Ś3 Things to learn while Programming

Anyway, if you been in computer programming for a while, you probably know that programs are build upon logics.
And a logic need to go though certain condition 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 straight forward:

if(this happen){
  do this particular task;
}else{
  do this.
}
Enter fullscreen mode Exit fullscreen mode

Which means, if certain condition met, do a particular task, and if the condition is not met, then
do something else.

let num = 1;
if(num > 0){ 
   console.log("The number is poditive");
} else {
   console.log("The number is negative");
}
Enter fullscreen mode Exit fullscreen mode

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.
}
Enter fullscreen mode Exit fullscreen mode

This syntax is similar to what we discuss 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");
}
Enter fullscreen mode Exit fullscreen mode

In the above example, we have checked that while dividing a number by 2, if we get 0 as reminder then
the number is even, and if we get 1 as reminder then the number is odd.

Note: '%' is the modulo operator which gives the reminder while divide a number,
whereas '/' gives the quotient.{#quote .info__quote}

else if

Apart from if...else, we have else if, which is use 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.
}
Enter fullscreen mode Exit fullscreen mode

In this syntax, you can easily see that here is an another condition to be check.

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 negetive");
}
Enter fullscreen mode Exit fullscreen mode

Quite obvious; In the above program we also check that 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 is a conditional statement and its type. But we have a another version
of conditional statement with nesting.

Nesting means checking a condition inside a particlular condition.

âť• Make Sence ? 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");
    }
}
Enter fullscreen mode Exit fullscreen mode

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 a if 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 kind of conditional statement:

  • if..else
  • else if

We also have a nested conditional statement, to check a condition inside a condition.

And this is all about conditional statement in JavaScript.

Top comments (0)