Avoid unnecessary if-else chains that only returns a boolean

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: javascript-best-practices/no-if-else-return

Language: JavaScript

Severity: Warning

Category: Best Practices

Description

This rule is designed to simplify your code by avoiding unnecessary if-else chains that only return a boolean. In JavaScript, it’s not necessary to use an if-else statement to return a boolean value from a function. Instead, you can return the result of the boolean expression. This makes your code shorter, cleaner, and easier to understand.

Non-Compliant Code Examples

function getFoo() {
  const foo = computeFoo();
  if (foo) {
    return true;
  } else {
    return false;
  }
}

Compliant Code Examples

function getFoo() {
  const foo = computeFoo();
  return foo;
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis