JS Built-ins
← Array77 / 226
ArrayArray.prototype.findLastbeginner

Array.prototype.findLast() β€” find last even number

πŸ“– Usage Example
const nums = [5, 12, 8, 130, 44]
nums.findLast(n => n > 10)  // β†’ 44  (last match)

Use findLast() to get the last element that satisfies a condition, searching from the end.

Array.prototype.findLast()

Array.prototype.findLast(callback) is the reverse of find() β€” it iterates from the end of the array and returns the last element for which the callback is truthy.

Challenge: Implement findLastEven(nums) that returns the last even number in the array.

findLastEven([1, 2, 3, 4, 5]) // β†’ 4
findLastEven([1, 3, 5])       // β†’ undefined
πŸ’‘ Hints (2)
  • β€’ `findLast()` scans from the end β€” the opposite direction from `find()`.
  • β€’ Returns `undefined` (not `null`) when no match is found.