unicorn/prefer-array-slice
eslint-plugin-unicorn
Suggestion
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-array-slice": "error",
},
},
];Prefer Array#slice() over Array#splice() when reading from the returned array.
const foo = process.argv.splice(2)[0];const foo = array.splice(index).at(0);const foo = process.argv.slice(2)[0];const foo = array.slice(index).at(0);array.splice(index);Examples from the ESLint documentation.
unicorn/prefer-array-slice
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-array-slice": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Prefer iterating an array directly or with Array#keys() over Array#entries() when the index or value is unused.
Prefer combining consecutive guards with identical exit statements.
Prefer Array.fromAsync() over array accumulation loops.
Prefer .getHTML() and .setHTML() over .innerHTML.
Prefer AbortSignal.timeout() over manually aborting an AbortController with setTimeout().
Prefer else if over adjacent if statements with related conditions.