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:
String#slice()Array#slice()TypedArray#slice()String#at()Array#at()TypedArray#at()Array#splice()Array#toSpliced()Array#with()TypedArray#with()TypedArray#subarray()
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]);