Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need a last `else` clause in an `if...else if` statement? [duplicate]

Every if...else if example I’ve seen includes a final else clause:

if (condition1) {
  doA();
} else if (condition2) {
  doB();
} else if (condition3) {
  doC();
} else {
  noConditionsMet();
}
alwaysDoThis();

I understand that this is basically syntactic sugar for nested if...else statements:

if (condition1) {
  doA();
} else {
  if (condition2) {
    doB();
  } else {
    if (condition3) {
      doC();
    } else {
      noConditionsMet();
    }
  }
}
alwaysDoThis();

I have never seen any examples of an if...else if that omits the last else clause. But seeing as plain if statements (without else clauses) are valid, and going by the equivalent “nested statements” above, my gut tells me that this is okay to do:

if (condition1) {
  doA();
} else if (condition2) {
  doB();
} else if (condition3) {
  doC();
}
alwaysDoThis();

Can someone point me to a resource or example that explicitly says whether or not it’s valid?

And on another level, if it is valid, would it be recommended or is it considered “bad practice”?

like image 893
chharvey Avatar asked Jul 22 '16 23:07

chharvey


People also ask

Is it necessary to have an ELSE clause in an IF statement?

An if statement looks at any and every thing in the parentheses and if true, executes block of code that follows. If you require code to run only when the statement returns true (and do nothing else if false) then an else statement is not needed.

Can we use if statement twice?

Answer 514a8bea4a9e0e2522000cf1You can use multiple else if but each of them must have opening and closing curly braces {} . You can replace if with switch statement which is simpler but only for comparing same variable.

Can you have an if else statement without an else?

In fact, it is very common to have an if without an else when there is no specific activity that needs to be performed when the condition is false.


3 Answers

The ending else is not mandatory as far as JavaScript is concerned. As for whether it is needed, it depends on what you want to achieve.

The trailing else clause will execute when none of the specified conditions is true. If the conditions are collectively exhaustive, then an else clause is entirely superfluous, except possibly to contain an assertion that catches the "impossible" condition. In your case, whether you need an else clause depends on whether you want specific code to run if and only if neither of condition1, condition2, and condition3 are true.

else can be omitted for any if statement, there is nothing special in the last if of an if/else if chain. This is documented in any JavaScript grammar, e.g. in the specification.

like image 151
user4815162342 Avatar answered Oct 17 '22 18:10

user4815162342


It is 100% valid. No, it is not bad practice. If you don't need it, don't write it.

Take the following for example:

function doStuff(data) {
    if (data.something) {
        // go format the data in some way
    }
    else if (data.somethingElse) {
        // go format the data in some other way
    }
    // must be formatted correctly, don't do anything else
    ...
}
like image 15
KevBot Avatar answered Oct 17 '22 17:10

KevBot


You never need an else clause. (It's hard to offer examples of something that is not necessary, so I'll leave it at that.)

edit as a comment notes, square brackets in language syntax notation usually indicate that something is optional.

like image 13
Pointy Avatar answered Oct 17 '22 18:10

Pointy