atAccesses
Prefer using .at() for accessing elements at negative indices.
✅ This rule is included in the ts stylistic preset.
Accessing an element at the end of an array or string using array[array.length - 1] is less readable than using the .at() method with a negative index.
The .at() method provides a cleaner syntax for accessing elements from the end of a sequence.
Examples
Section titled “Examples”const last = array[array.length - 1];const secondLast = items[items.length - 2];const lastChar = text[text.length - 1];const last = array.at(-1);const secondLast = items.at(-2);const lastChar = text.at(-1);const first = array[0];const element = array[dynamicIndex];Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you need to support environments that do not have the .at() method (pre-ES2022), or if your project has a polyfill that only covers certain cases, you can disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useAtIndex - ESLint:
unicorn/prefer-at
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.