Javascript Else If Statement – Learn The Concept Of Else If In Javascript

What is Javascript Else If Statement?

In the previous tutorial, we learn about if and else statements. In this article, I will explain the concept of javascript else if statement in simple words.

The if statement executes statements when the condition evaluates to true and the else statement executes when the condition evaluates to false.

But what is else if statement. What’s the exact use of else if statement.?

Javascript else if statement executes a multiple if conditions in a row until it reaches the correct condition.

In simple words javascript else if statement is nothing but a chain of if statements. Because we form a chain of if blocks. But each if block after the first if block should be prefixed with an else keyword.

Example:          else if 

Let’s have a look at the syntax before going to know about it.

Syntax :

if( expression ){
   //executes code if true
}else if( expression ){
   //executes code if else if true
}else if( expression ){
   //executes code if else if true
}else{
   //executes code if all the if and else if fail
}

How it works :

  1. Firstly the javascript interpreter executes the if statement.
  2. If the condition is true, the block followed by if statement will be executed.
  3. In case, the condition is false, the else if block followed by if block will be executed.
  4. If the else if statement also evaluates false, the next else if statement will be executed. This process repeated continuously until the expression evaluates to true.
  5. Finally, if all the else if statements evaluate to false, the else block will be executed.

See the picture below for better understanding.

flowchart of javascript else if statement

Note: You can write else if statements as many as you want. Each one is executed one by one until it reaches the right condition.

Example :

var n = 3;

if( n == 1 ){
   console.log(1);
}else if( n == 2 ){
   console.log(2);
}else if(n == 3 ){
   console.log(3);
}else{
   console.log("All the conditions were wrong");
}


//expected output - 2

In the above example, the interpreter first checks the if condition. If it is false, then checks the else if statement immediately followed by if statement.

It checks all the else if statements until it find the correct expression which evaluates to true.

If all the if and else if statements evaluate to false, then it goes with the else statement.

What’s the difference between else and else if?

The else statement executes a block of code when the if condition is false. The else if statement executes another condition when the if condition is false rather then executing the else block.

Ok. That’s it for the javascript else if statement.

Hoping, now you understood the concept of javascript else if statement. If you have any doubts feel free to comment below.

If you like this article please share with your friends.

Thanks for reading!

Keep [email protected]

Was this post helpful?
Let us know if you liked the post. That’s the only way we can improve.
Yes0
No0
Powered by Pixelbart

Leave a Reply

Your email address will not be published. Required fields are marked *