“返回"后不必要的“其他".(无其他返回) [英] Unnecessary 'else' after 'return'. (No-else-return)

查看:36
本文介绍了“返回"后不必要的“其他".(无其他返回)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 es-lint 来清理代码中的错误.我遇到了这个错误:

I am using es-lint to clean up the errors in my code. I have come across this error:

'return' 后不必要的'else'.(无其他返回)

Unnecessary 'else' after 'return'. (No-else-return)

} else {

我总是在返回后使用 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);

推荐答案

这基本上是说,如果 中有 return,则 if 语句的 else 部分是不必要的如果 部分.它所期望的是这样的:

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;

一般来说,这是:

if (condition) {
  return something;
} else {
  // do another thing
}

return anotherThing;

类似于:

if (condition) {
  return something;
}

// do another thing
return anotherThing;

在带有return语句的if之后,不需要else部分作为if<下面的代码/code> 只会在不满足所述条件时运行.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆