Skip to content

useNegativeIndex (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useNegativeIndex": "error"
}
}
}
}

Prefer negative indexes over subtracting from .length.

Methods such as slice() and at() accept negative indexes, which count back from the end. array.at(-1) is easier to read than array.at(array.length - 1).

The rule checks slice(), at(), subarray(), splice(), and toSpliced(), including calls through call() and apply(), such as Array.prototype.slice.call(array, array.length - 1).

The fix is unsafe, because the rule can’t tell whether the value is really an array or a string. Also, subtracting more than the length gives a different result: [1, 2, 3].slice(3 - 4) returns [3], while [1, 2, 3].slice(-4) returns the whole array.

foo.slice(foo.length - 2, foo.length - 1);
code-block.js:1:11 lint/nursery/useNegativeIndex  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This slice() call subtracts from .length to count from the end.

> 1 │ foo.slice(foo.length - 2, foo.length - 1);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ slice() accepts negative indexes, which count back from the end, so the subtraction makes the position harder to read.

ℹ Use a negative index instead, such as -1 for array.length - 1.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use a negative index.

1 │ foo.slice(foo.length·-·2,·foo.length·-·1);
│ ----------- - ----------- -
foo.splice(foo.length - 1, 1);
code-block.js:1:12 lint/nursery/useNegativeIndex  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This splice() call subtracts from .length to count from the end.

> 1 │ foo.splice(foo.length - 1, 1);
│ ^^^^^^^^^^^^^^
2 │

ℹ splice() accepts negative indexes, which count back from the end, so the subtraction makes the position harder to read.

ℹ Use a negative index instead, such as -1 for array.length - 1.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use a negative index.

1 │ foo.splice(foo.length·-·1,·1);
│ ----------- -
foo.at(foo.length - 1);
code-block.js:1:8 lint/nursery/useNegativeIndex  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This at() call subtracts from .length to count from the end.

> 1 │ foo.at(foo.length - 1);
│ ^^^^^^^^^^^^^^
2 │

ℹ at() accepts negative indexes, which count back from the end, so the subtraction makes the position harder to read.

ℹ Use a negative index instead, such as -1 for array.length - 1.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use a negative index.

1 │ foo.at(foo.length·-·1);
│ ----------- -
Array.prototype.slice.call(foo, foo.length - 2, foo.length - 1);
code-block.js:1:33 lint/nursery/useNegativeIndex  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This slice() call subtracts from .length to count from the end.

> 1 │ Array.prototype.slice.call(foo, foo.length - 2, foo.length - 1);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ slice() accepts negative indexes, which count back from the end, so the subtraction makes the position harder to read.

ℹ Use a negative index instead, such as -1 for array.length - 1.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use a negative index.

1 │ Array.prototype.slice.call(foo,·foo.length·-·2,·foo.length·-·1);
│ ----------- - ----------- -
Array.prototype.slice.apply(foo, [foo.length - 2, foo.length - 1]);
code-block.js:1:35 lint/nursery/useNegativeIndex  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This slice() call subtracts from .length to count from the end.

> 1 │ Array.prototype.slice.apply(foo, [foo.length - 2, foo.length - 1]);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ slice() accepts negative indexes, which count back from the end, so the subtraction makes the position harder to read.

ℹ Use a negative index instead, such as -1 for array.length - 1.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Use a negative index.

1 │ Array.prototype.slice.apply(foo,·[foo.length·-·2,·foo.length·-·1]);
│ ----------- - ----------- -
foo.slice(-2, -1);
foo.splice(-1, 1);
foo.at(-1);
Array.prototype.slice.call(foo, -2, -1);
Array.prototype.slice.apply(foo, [-2, -1]);
foo.slice(bar.length - 1);
foo.with(foo.length - 1, value);
  • If you want to use at() instead of reading an element by its index, see useAtIndex.