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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Unnecessary ‘else’ after ‘return’. (No-else-return) without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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.

We are here to answer your question about Unnecessary ‘else’ after ‘return’. (No-else-return) - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji