Browse topics

Array.prototype.findLast

Version
es2025
Since
es2023
Review
Draft

Summary#

Array.prototype.findLast scans from the end and returns the first value that passes a predicate. It leaves the input order intact.

Syntax#

js
array.findLast((value, index, source) => matches(value))

Example#

js
const jobs = [
  { name: "lint", complete: true },
  { name: "test", complete: false },
  { name: "build", complete: true },
];

const lastComplete = jobs.findLast((job) => job.complete);
console.log(lastComplete?.name);
console.log(jobs.findLast((job) => job.name === "deploy"));

Expected output#

Text
build
undefined

Common mistakes#

Use findLastIndex when you need the position. reverse() changes the input array, and filter(...).at(-1) evaluates the predicate for every present element. findLast reads every index it visits, so the predicate can also receive undefined from an empty slot.

Version applicability#

ES2023 added findLast. On ES2022 and earlier runtimes, use a descending index loop or an appropriate polyfill without reversing the input in place.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0