prefer-array-slice

June 14, 2026 Β· View on GitHub

πŸ“ Prefer Array#slice() over Array#splice() when reading from the returned array.

πŸ’ΌπŸš« This rule is enabled in the βœ… recommended config. This rule is disabled in the β˜‘οΈ unopinionated config.

πŸ’‘ This rule is manually fixable by editor suggestions.

Prefer Array#slice() over Array#splice() when reading from the returned array.

Array#splice() mutates the source array. When the returned elements are immediately indexed or read with .at(), Array#slice() expresses the read-only intent without changing the source array.

Examples

// ❌
const foo = process.argv.splice(2)[0];

// βœ…
const foo = process.argv.slice(2)[0];
// ❌
const foo = array.splice(index).at(0);

// βœ…
const foo = array.slice(index).at(0);
// βœ…
array.splice(index);
// βœ…
array.splice(index, deleteCount)[0];

Keep Array#splice() when intentional mutation is part of the operation.