# prefer-else-if

Rule `unicorn/prefer-else-if`. Prefer `else if` over adjacent `if` statements with related conditions.

- 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: #1027 by reach, #1282 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-else-if` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-else-if.md) |
| Biome | `nursery/useElseIf` | no | unsafe | group nursery, since 2.6.10 | [docs](https://biomejs.dev/linter/rules/use-else-if/) |

## Code examples

### Incorrect: the rule reports this

```js
if (foo === 1) {
  one();
}

if (foo === 2) {
  two();
}
```

### Correct: the rule accepts this

```js
if (foo === 1) {
  one();
} else if (foo === 2) {
  two();
}
```

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-else-if": "error",
    },
  },
];
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "nursery": {
        "useElseIf": "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% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-continue](https://lintradar.dev/rules/unicorn/prefer-continue.md): Prefer early continues over conditionals wrapping the remainder of the loop body.
- [unicorn/prefer-iterator-zip](https://lintradar.dev/rules/unicorn/prefer-iterator-zip.md): Prefer `Iterator.zip()` over parallel-array indexing.
- [unicorn/no-useless-recursion](https://lintradar.dev/rules/unicorn/no-useless-recursion.md): Disallow simple recursive function calls that can be replaced with a loop.
- [unicorn/prefer-set-methods](https://lintradar.dev/rules/unicorn/prefer-set-methods.md): Prefer `Set` methods for Set operations.
- [unicorn/no-mismatched-map-key](https://lintradar.dev/rules/unicorn/no-mismatched-map-key.md): Disallow checking a Map key before accessing a different key.
- [unicorn/class-reference-in-static-methods](https://lintradar.dev/rules/unicorn/class-reference-in-static-methods.md): Enforce consistent class references in static methods.

---

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