Course contentsShow
← JavaScript
Lesson 475 of 2,72312. Higher-Order FunctionsFree lesson

FindLast and FindLastIndex

Learn the modern findLast() and findLastIndex() methods for searching arrays from the end.

FindLast and FindLastIndex

You'll learn how to search arrays from the end using modern JavaScript methods that mirror find() and findIndex().

Searching Backwards

While find() and findIndex() search from the beginning of an array, sometimes you need to locate the last occurrence of something. That's where findLast() and findLastIndex() come in—they work exactly like their forward-searching cousins, but start from the end and move toward the beginning.

Think of it like looking for your car in a parking lot. If you know you parked near the exit (the end), you'd search backward from there rather than starting at the entrance.

How They Work

findLast() returns the value of the last element that satisfies your test function:

const scores = [45, 78, 92, 88, 95];
const lastHighScore = scores.findLast(score => score > 90);
// 95 (searches right-to-left, finds 95 before 92)

findLastIndex() returns the index of that last matching element:

const scores = [45, 78, 92, 88, 95];
const lastHighScoreIndex = scores.findLastIndex(score => score > 90);
// 4 (the position of 95)

Both return undefined (for findLast()) or -1 (for findLastIndex()) if nothing matches.

When to Use Them

These methods shine when:

  • You need the most recent entry in time-ordered data
  • You're working with duplicates and care about the last one
  • Searching backward is logically more efficient
const history = ['home', 'profile', 'settings', 'home'];
const lastHomeIndex = history.findLastIndex(page => page === 'home');
// 3 (the second 'home', not the first)

Key Takeaway: findLast() and findLastIndex() are backward-searching versions of find() and findIndex(), perfect for locating the last occurrence in an array.