unicorn/prefer-at
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-at": "error",
},
},
];const foo = array[array.length - 1];const foo = array.slice(-1)[0];const foo = array.slice(-1).pop();const foo = array.at(-1);const foo = array.at(-5);const foo = string.at(-5);Examples from the ESLint documentation.
unicorn/prefer-at
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-at": "error",
},
},
];unicorn/prefer-at
unicorn plugin · pedantic
{
"rules": {
"unicorn/prefer-at": "error"
}
}style/useAtIndex
style · since 1.9.4
{
"linter": {
"rules": {
"style": {
"useAtIndex": "error"
}
}
}
}Prefer .some(…) over .filter(…).length check and .{find,findLast,findIndex,findLastIndex}(…).
Prefer JavaScript modules (ESM) over CommonJS.
Enforce the style of numeric separators by correctly grouping digits.
Prefer RegExp#test() over String#match(), String#search(), and RegExp#exec().
Disallow if statements as the only statement in if blocks without else.
Prefer String#slice() over String#substr() and String#substring().