# prefer-negative-index

Rule `unicorn/prefer-negative-index`. Prefer negative index over `.length - index` when possible.

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

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-negative-index` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-negative-index.md) |
| Oxlint | `unicorn/prefer-negative-index` | no | safe | plugin `unicorn`, category style | [docs](https://oxc.rs/docs/guide/usage/linter/rules/unicorn/prefer-negative-index.html) |
| Biome | `nursery/useNegativeIndex` | no | unsafe | group nursery, since 2.6.10 | [docs](https://biomejs.dev/linter/rules/use-negative-index/) |

## Code examples

### Incorrect: the rule reports this

```js
foo.slice(foo.length - 2, foo.length - 1);
```

```js
foo.splice(foo.length - 1, 1);
```

```js
foo.at(foo.length - 1);
```

### Correct: the rule accepts this

```js
foo.slice(-2, -1);
```

```js
foo.splice(-1, 1);
```

```js
foo.at(-1);
```

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

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "unicorn/prefer-negative-index": "error"
  }
}
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "nursery": {
        "useNegativeIndex": "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% |
| [Ultracite for Oxlint](https://lintradar.dev/presets/ultracite-oxlint.md) | Oxlint | `ultracite` | 475 | 2.1M | +151.7% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/no-zero-fractions](https://lintradar.dev/rules/unicorn/no-zero-fractions.md): Require consistent decimal numbers without redundant zeros.
- [unicorn/prefer-query-selector](https://lintradar.dev/rules/unicorn/prefer-query-selector.md): Prefer `.querySelector()` and `.querySelectorAll()` over older DOM query methods.
- [unicorn/no-unreadable-iife](https://lintradar.dev/rules/unicorn/no-unreadable-iife.md): Disallow unreadable IIFEs.
- [unicorn/explicit-timer-delay](https://lintradar.dev/rules/unicorn/explicit-timer-delay.md): Enforce or disallow explicit `delay` argument for `setTimeout()` and `setInterval()`.
- [unicorn/no-static-only-class](https://lintradar.dev/rules/unicorn/no-static-only-class.md): Disallow classes that only have static members.
- [unicorn/no-abusive-eslint-disable](https://lintradar.dev/rules/unicorn/no-abusive-eslint-disable.md): Enforce specifying rules to disable in `eslint-disable` comments.

---

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