# no-declarations-before-early-exit

Rule `unicorn/no-declarations-before-early-exit`. Disallow declarations before conditional early exits when they are only used after the exit.

- 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: #921 by reach, #1176 by growth

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
function foo(bar) {
  const result = 1;
  if (!bar) {
    return;
  }

  console.log(result);
}
```

```js
function foo(bar) {
  const result = 1;
  if (!bar) {
    throw new Error();
  }

  console.log(result);
}
```

```js
function foo(bar) {
  const result = getResult();
  if (!bar) {
    return;
  }

  console.log(result);
}
```

### Correct: the rule accepts this

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

  const result = 1;
  console.log(result);
}
```

```js
function foo(bar) {
  if (!bar) {
    throw new Error();
  }

  const result = 1;
  console.log(result);
}
```

Examples from the ESLint documentation.

## Enable it

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

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

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/no-declarations-before-early-exit": "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/no-conflicting-constraints](https://lintradar.dev/rules/unicorn/no-conflicting-constraints.md): Disallow conflicting CSS query and HTML form constraints.
- [unicorn/no-duplicate-logical-operands](https://lintradar.dev/rules/unicorn/no-duplicate-logical-operands.md): Disallow adjacent duplicate operands in logical expressions.
- [unicorn/no-chained-comparison](https://lintradar.dev/rules/unicorn/no-chained-comparison.md): Disallow chained comparisons such as `a < b < c`.
- [unicorn/no-error-property-assignment](https://lintradar.dev/rules/unicorn/no-error-property-assignment.md): Disallow assigning to built-in error properties.
- [unicorn/no-blob-to-file](https://lintradar.dev/rules/unicorn/no-blob-to-file.md): Disallow unnecessary `Blob` to `File` conversion.
- [unicorn/no-exports-in-scripts](https://lintradar.dev/rules/unicorn/no-exports-in-scripts.md): Disallow exports in scripts.

---

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