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”?
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.
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.
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.
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.
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
...
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With