Course contentsShow
← JavaScript
Lesson 1996 of 2,72341. Modern JavaScript Features (ES2020+)Free lesson

findLast() Use Cases

Explore practical scenarios where searching from the end is more efficient, like finding the most recent matching item.

findLast() Use Cases

What you'll learn: How to recognize situations where findLast() is more efficient and clearer than searching from the beginning of an array.

When Searching from the End Makes Sense

While find() is great for locating items, many real-world scenarios benefit from searching backward. Think of it like searching through a stack of papers: if you need the most recent document, you start from the top (the end), not the bottom (the beginning).

Practical Use Cases

Finding the Most Recent Match

When working with time-ordered data, the latest item is often at the end:

const transactions = [
  { id: 1, amount: 50, status: 'pending' },
  { id: 2, amount: 100, status: 'completed' },
  { id: 3, amount: 75, status: 'pending' }
];

// Find the most recent pending transaction
const lastPending = transactions.findLast(t => t.status === 'pending');
// { id: 3, amount: 75, status: 'pending' }

Logs and Event History

Logs are typically appended to the end, so recent events are found faster:

const logs = [
  { time: '10:00', level: 'info', msg: 'Started' },
  { time: '10:15', level: 'error', msg: 'Failed' },
  { time: '10:30', level: 'error', msg: 'Retry failed' }
];

const lastError = logs.findLast(log => log.level === 'error');
// Starts from 10:30, finds it immediately

Stacks and Undo Operations

When managing user actions in reverse order:

const edits = ['typed "hello"', 'deleted char', 'typed "world"', 'deleted char'];

const lastDeletion = edits.findLast(edit => edit.includes('deleted'));
// Finds 'deleted char' (the most recent one)

Why It Matters

Using findLast() isn't just about convenience—it can be meaningfully faster when matches are likely near the end. Instead of iterating through hundreds or thousands of elements, you hit the target immediately.

Key Takeaway: Use findLast() when your data is time-ordered, stack-like, or when recent items matter most—it's both clearer in intent and more efficient than searching from the start.