Skip to content

Files

Latest commit

 

History

History
49 lines (41 loc) · 814 Bytes

no-else-return.md

File metadata and controls

49 lines (41 loc) · 814 Bytes

Pattern: else block after return in if statement

Issue: -

Description

When an if block contains a return statement, the subsequent else block becomes unnecessary. The code in the else block can be placed outside the if statement since the return already exits the function when the if condition is true.

Examples

Example of incorrect code:

function foo() {
  if (x) {
    return y;
  } else {
    return z;
  }
}

function process(error) {
  if (error) {
    return "Failed";
  } else {
    if (loading) {
      return "Loading";
    }
  }
}

Example of correct code:

function foo() {
  if (x) {
    return y;
  }
  return z;
}

function process(error) {
  if (error) {
    return "Failed";
  }
  if (loading) {
    return "Loading";
  }
}