Skip to content

Latest commit

Β 

History

History
44 lines (30 loc) Β· 1.27 KB

File metadata and controls

44 lines (30 loc) Β· 1.27 KB

prefer-array-slice

πŸ“ 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.