Unnecessary ‘else’ after ‘return’. (No-else-return)

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);
}
module.exports = calculateCredit;

Answer

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.

Leave a Reply

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