Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/sindresorhus/eslint-plugin-unicorn/llms.txt

Use this file to discover all available pages before exploring further.

Use Array#at(), String#at(), and equivalent methods on typed arrays and DOM collections for index access. The .at() method is cleaner for negative indices in particular, removing the need to reference .length manually. It also replaces String#charAt().
Recommendedrecommended · ☑️ unopinionated
Fixable🔧 Auto-fixable
Suggestions💡 Has suggestions
By default this rule only checks negative index access (e.g., array[array.length - 1]). Enable checkAllIndexAccess to also enforce .at() for positive integer indices.

Examples

// Negative index access
const foo = array[array.length - 1];

// Using .slice() to get last element
const foo = array.slice(-1)[0];
const foo = array.slice(-1).pop();
const foo = array.slice(-1).shift();

// Lodash helper (always checked)
const foo = lodash.last(array);

// String#charAt with negative-style index
const foo = string.charAt(string.length - 5);

Options

Type: object

checkAllIndexAccess

Type: boolean
Default: false
When true, the rule also reports positive integer index access that can use .at().
/* eslint unicorn/prefer-at: ["error", {"checkAllIndexAccess": true}] */

const foo = bar[10]; // Fails — fixed to bar.at(10)
const foo = bar[unknownProperty]; // Passes — not a static integer
const foo = string.charAt(unknownIndex); // Fails

getLastElementFunctions

Type: string[]
Default: []
Additional function names or paths to treat as “get last element” helpers. _.last(), lodash.last(), and underscore.last() are always included.
/* eslint unicorn/prefer-at: ["error", {"getLastElementFunctions": ["utils.lastElement"]}] */

// Fails — fixed to bar.at(-1)
const foo = utils.lastElement(bar);
The related rule unicorn/prefer-negative-index enforces using negative indices with .at() as well.

Build docs developers (and LLMs) love