# prefer-early-return

Rule `unicorn/prefer-early-return`. Prefer early returns over conditionals wrapping the remainder of the function body.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 9.9M weekly downloads of presets that enable it
- Trend: +28.3% last quarter
- Rank: #974 by reach, #1229 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-early-return` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-early-return.md) |

## Code examples

### Incorrect: the rule reports this

```js
function foo() {
  doSomethingBefore();
  if (condition) {
    doSomething();
    doSomethingElse();
  }
}
```

### Correct: the rule accepts this

```js
function foo() {
  doSomethingBefore();
  if (!condition) {
    return;
  }

  doSomething();
  doSomethingElse();
}
```

```js
function foo() {
  if (condition) {
    doSomething();
  }
}
```

```js
function foo() {
  if (condition) {
    doSomething();
    doSomethingElse();
  }

  finish();
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-early-return": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Unicorn unopinionated](https://lintradar.dev/presets/unicorn-unopinionated.md) | ESLint | `eslint-plugin-unicorn` | 236 | 9.5M | +30.3% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-dom-node-replace-children](https://lintradar.dev/rules/unicorn/prefer-dom-node-replace-children.md): Prefer `.replaceChildren()` when replacing DOM children.
- [unicorn/prefer-escaped-irregular-whitespace](https://lintradar.dev/rules/unicorn/prefer-escaped-irregular-whitespace.md): Prefer escape sequences for irregular whitespace characters.
- [unicorn/prefer-direct-iteration](https://lintradar.dev/rules/unicorn/prefer-direct-iteration.md): Prefer direct iteration over default iterator method calls.
- [unicorn/prefer-global-number-constants](https://lintradar.dev/rules/unicorn/prefer-global-number-constants.md): Prefer global numeric constants over `Number` static properties.
- [unicorn/prefer-await](https://lintradar.dev/rules/unicorn/prefer-await.md): Prefer `await` over promise chaining.
- [unicorn/prefer-has-check](https://lintradar.dev/rules/unicorn/prefer-has-check.md): Prefer `.has()` when checking existence.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-early-return. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
