JS: Array.prototype.findLast

By Xah Lee. Date: .

(new in JS: ECMAScript 2023)

similar to JS: Array.prototype.find but find start from the last item.

const isEven = (x) => (x % 2 === 0 ? true : false);

console.log([7, 3, 4, 2].findLast(isEven) === 2);

// if not found, return undefined
console.log([1, 3, 5].findLast(isEven) === undefined);
// get the last even value whose index is also even
const bothEven = (x, i) => (x % 2 === 0 && i % 2 === 0 ? true : false);
console.log([7, 2, 2, 4, 6].findLast(bothEven) === 6);

JavaScript. Array Search