prefer-negative-index

March 27, 2026 ยท View on GitHub

๐Ÿ“ Prefer negative index over .length - index when possible.

๐Ÿ’ผ This rule is enabled in the following configs: โœ… recommended, โ˜‘๏ธ unopinionated.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

Prefer negative index over calculating from .length for:

Examples

// โŒ
foo.slice(foo.length - 2, foo.length - 1);

// โœ…
foo.slice(-2, -1);
// โŒ
foo.splice(foo.length - 1, 1);

// โœ…
foo.splice(-1, 1);
// โŒ
foo.at(foo.length - 1);

// โœ…
foo.at(-1);
// โŒ
Array.prototype.slice.call(foo, foo.length - 2, foo.length - 1);

// โœ…
Array.prototype.slice.call(foo, -2, -1);
// โŒ
Array.prototype.slice.apply(foo, [foo.length - 2, foo.length - 1]);

// โœ…
Array.prototype.slice.apply(foo, [-2, -1]);