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;
}