I am using es-lint to clean up the errors in my code. I have come across this error:
Unnecessary 'else' after 'return'. (No-else-return)
} else {
I have always used else statements after a return. Is there something I may be overlooking?
if (cctot <= 3 && cctot > 0) { alert('Credit under $3.00 not allowed'); return Number.MIN_SAFE_INTEGER; // important to return 0 so we can check for these conditions for validation } else { cctot *= -1; } return precise(cctot);
At the time of writing this, the Mozilla Coding Style guidelines have this recommendation under “General C/C++ Practices”: Don't put an else right after a return. Delete the else, it's unnecessary and increases indentation level.
It's just saying that there's no need for 'else' since the execution of the function already stopped and if the 'if' condition doesn't succeed, it will still run any code underneath it.
To put it simply a continuation is a function which is used in place of a return statement. More formally: A continuation is a function which is called by another function. The last thing the other function does is call the continuation.
You don't need an else statement, you can use ng-if . The ng-if statement is also available without an else condition.
What that is basically saying is that the else part of the if statement is unnecessary if there is a return
in the if
part. Something like this is what it expects:
if (cctot <= 3 && cctot > 0) { alert('Credit under $3.00 not allowed'); return Number.MIN_SAFE_INTEGER; // important to return 0 so we can check for these conditions for validation } cctot *= -1;
In general, this:
if (condition) { return something; } else { // do another thing } return anotherThing;
is similar to:
if (condition) { return something; } // do another thing return anotherThing;
After the if
with a return
statement, there is no need for the else
part as the code below the if
will only run when the condition stated is not fulfilled.
It's a code style preference. You don't need the else
and instead can put the else
code directly below the if
. This is because if the if
succeeds, that's the end of the function, so the else
code will never be reached anyway.
So this:
if (condition) { return foo; } else { // do bar } return baz
is equivalent to this:
if (condition) { return foo; } // do bar return baz
This style seems to vary in different programming communities. Go developers will nearly always omit the else
, while I've seen more JS devs include it.
While I prefer to leave off the else
, it is again purely a matter of preference. Don't let it worry you too much. People may get dogmatic about this kind of thing, but it's really not that important.
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