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#find() and Array#findLast() instead of filtering an array and then accessing the first or last element. find() stops iterating as soon as it finds a match and never allocates a new array — making it both clearer in intent and more efficient.
Recommendedrecommended · ☑️ unopinionated
Fixable🔧 Auto-fixable
Suggestions💡 Has suggestions
This rule is fixable unless default values are used in destructuring. When a default value is present (e.g., const [item = fallback] = array.filter(…)), a suggestion is offered instead to choose between ?? or ||.

Examples

const item = array.filter(x => isUnicorn(x))[0];

const item = array.filter(x => isUnicorn(x)).shift();

const [item] = array.filter(x => isUnicorn(x));

// Last element patterns (when checkFromLast is true)
const item = array.filter(x => isUnicorn(x)).at(-1);

const item = array.filter(x => isUnicorn(x)).pop();

Options

Type: object

checkFromLast

Type: boolean
Default: true
Controls whether the rule also checks patterns that can be replaced with findLast(). Set to false to allow .filter(…).at(-1) and .filter(…).pop().
/* eslint unicorn/prefer-array-find: ["error", {"checkFromLast": false}] */

// Passes — not reported when checkFromLast is false
const item = array.filter(x => isUnicorn(x)).at(-1);
const item = array.filter(x => isUnicorn(x)).pop();

Build docs developers (and LLMs) love