jsguides

Array.prototype.findLast()

findLast(callbackFn, thisArg)

findLast() returns the last element in an array that satisfies a testing function, or undefined if no element matches. It iterates from the end of the array toward the beginning.

Syntax

findLast(callbackFn)
findLast(callbackFn, thisArg)

Parameters

ParameterTypeDefaultDescription
callbackFnfunction-A function to execute for each element. Return true to signal a match.
thisArganyundefinedOptional. Value to use as this when executing callbackFn.

callbackFn Parameters

ParameterTypeDescription
elementanyThe current element being processed.
indexnumberThe index of the current element.
arrayarrayThe array that findLast was called on.

Examples

Basic usage

const nums = [5, 12, 8, 130, 44];

const lastEven = nums.findLast(n => n % 2 === 0);
console.log(lastEven);
// 44

Basic value searches cover the simplest case, but the real strength of findLast() is searching arrays of objects where the match depends on a property rather than the element itself. You pass a callback that inspects whatever field you need, and the method handles the reverse iteration for you.

Finding the last matching object

const users = [
  { id: 1, name: "Alice", active: true },
  { id: 2, name: "Bob", active: false },
  { id: 3, name: "Charlie", active: true },
  { id: 4, name: "Diana", active: true }
];

const lastActive = users.findLast(u => u.active);
console.log(lastActive);
// { id: 4, name: "Diana", active: true }

When the array contains multiple matches, findLast() returns the one closest to the end — often the most recent entry in time-ordered data. It stops iterating as soon as a callback returns truthy, so earlier elements are never visited.

When no match is found

const nums = [1, 3, 5, 7];

const result = nums.findLast(n => n > 10);
console.log(result);
// undefined

A return value of undefined is the standard signal that no element passed the test. Always check for it before using the result, especially when the array contents are dynamic or user-supplied.

Common Patterns

Getting the last item satisfying a condition

When arrays are ordered by time — event logs, message queues, or audit trails — the last matching entry is usually the one you care about. findLast() expresses that intent without copying or reversing the array.

Use findLast when you need the most recent entry in a time-ordered array:

const events = [
  { type: "click", timestamp: 1700000000 },
  { type: "hover", timestamp: 1700000100 },
  { type: "click", timestamp: 1700000200 },
  { type: "scroll", timestamp: 1700000300 }
];

const lastClick = events.findLast(e => e.type === "click");
console.log(lastClick);
// { type: "click", timestamp: 1700000200 }

How findLast() works

findLast() iterates the array in reverse order - from length - 1 down to 0 - passing each element to the callback. The first element for which the callback returns a truthy value is immediately returned and iteration stops. If no element matches, findLast() returns undefined. The callback receives three arguments: the current element, its index in the original array, and the array itself.

findLast() vs reverse().find()

Before findLast() was introduced in ES2023, the common workaround was arr.slice().reverse().find(fn). The problem with that approach is that it allocates a new reversed copy of the entire array just to find one element. findLast() iterates from the end without copying and stops as soon as a match is found, making it both more memory-efficient and more readable.

const nums = [1, 3, 5, 7, 9];

// Old way - copies the whole array
nums.slice().reverse().find(n => n < 8); // 7

// Modern way - no copy, stops early
nums.findLast(n => n < 8); // 7

Why reverse search can be faster

Scanning from the end is not just a different API shape; it can also be a better fit for recent-first data. Logs, undo stacks, and append-only histories often place the most relevant item near the end, so a reverse search may stop earlier than a forward search would. findLast() expresses that intent directly and avoids the extra array copy that older reverse-and-search patterns needed.

Choosing Between findLast() and findLastIndex()

If you only need the value, findLast() is the simpler option. If you plan to remove, replace, or inspect the position later, findLastIndex() is usually the better choice. The two methods answer different questions, and choosing the one that matches your next step keeps the rest of the code smaller. A good rule of thumb is to pick the one that gives you exactly the data you need, not more.

findLast() vs findLastIndex()

findLast() returns the matching element; findLastIndex() returns the index of the matching element. Use findLastIndex() when you need the position to later modify or remove the element.

Browser support

findLast() is part of ES2023 and is supported in Chrome 97+, Firefox 104+, Safari 15.4+, and Node.js 18+. For older environments, use the slice().reverse().find() pattern or a polyfill.

See Also