"elseif" syntax in JavaScript

JavascriptIf StatementSyntax

Javascript Problem Overview


How can I achieve an elseif in a JavaScript condition?

Javascript Solutions


Solution 1 - Javascript

In JavaScript's if-then-else there is technically no elseif branch.

But it works if you write it this way:

if (condition) {

} else if (other_condition) {

} else {

}

To make it obvious what is really happening you can expand the above code using an additional pair of { and }:

if (condition) {

} else {

   if (other_condition) {

   } else {

   }

}

In the first example we're using some implicit JS behavior about {} uses. We can omit these curly braces if there is only one statement inside. Which is the case in this construct, because the inner if-then-else only counts as one statment. The truth is that those are 2 nested if-statements. And not an if-statement with 2 branches, as it may appear on first sight.

This way it resembles the elseif that is present in other languages.

It is a question of style and preference which way you use it.

Solution 2 - Javascript

Just add a space:

if (...) {

} else if (...) {

} else {

}

Solution 3 - Javascript

You could use this syntax which is functionally equivalent:

switch (true) {
  case condition1:
     //e.g. if (condition1 === true)
     break;
  case condition2:
     //e.g. elseif (condition2 === true)
     break;
  default:
     //e.g. else
}

This works because each condition is fully evaluated before comparison with the switch value, so the first one that evaluates to true will match and its branch will execute. Subsequent branches will not execute, provided you remember to use break.

Note that strict comparison is used, so a branch whose condition is merely "truthy" will not be executed. You can cast a truthy value to true with double negation: !!condition.

Solution 4 - Javascript

Actually, technically when indented properly, it would be:

if (condition) {
    ...
} else {
    if (condition) {
        ...
    } else {
        ...
    }
}

There is no else if, strictly speaking.

(Update: Of course, as pointed out, the above is not considered good style.)

Solution 5 - Javascript

if ( 100 < 500 ) {
   //any action
}
else if ( 100 > 500 ){
   //any another action
}

Easy, use space

Solution 6 - Javascript

Conditional statements are used to perform different actions based on different conditions.

Use if to specify a block of code to be executed, if a specified condition is true

Use else to specify a block of code to be executed, if the same condition is false

Use else if to specify a new condition to test, if the first condition is false

Solution 7 - Javascript

x = 10;
if(x > 100 ) console.log('over 100')
else if (x > 90 ) console.log('over 90')
else if (x > 50 ) console.log('over 50')
else if (x > 9 ) console.log('over 9')
else console.log('lower 9')	

Solution 8 - Javascript

You are missing a space between else and if

It should be else if instead of elseif

if(condition)
{

} 
else if(condition)
{

}
else
{

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHari GillalaView Question on Stackoverflow
Solution 1 - JavascriptJeffView Answer on Stackoverflow
Solution 2 - JavascriptjMylesView Answer on Stackoverflow
Solution 3 - JavascriptTamlynView Answer on Stackoverflow
Solution 4 - JavascriptskubeView Answer on Stackoverflow
Solution 5 - JavascriptIdemeNaHavajView Answer on Stackoverflow
Solution 6 - JavascriptA.A NomanView Answer on Stackoverflow
Solution 7 - JavascriptzloctbView Answer on Stackoverflow
Solution 8 - JavascriptcodemirrorView Answer on Stackoverflow