---
url: /docs/guide/usage/linter/rules/unicorn/prefer-negative-index.md
---

### 作用

在可能的情况下，优先使用负索引而不是 `.length - index`。

### 为什么这不好？

在 `at` 或 `slice` 中使用负索引通常比使用 `.length - index` 更易读
也更简洁。

### 示例

此规则的**错误**代码示例：

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

此规则的**正确**代码示例：

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

## 如何使用

## 版本

此规则于 v0.13.2 中添加。

## 参考
