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

Array.prototype.findLast() β€” returns undefined when not found

πŸ“– Usage Example
const arr = [1, 2, 3]
arr.findLast(n => n > 10)  // β†’ undefined  (no match)

findLast() returns undefined when no element satisfies the predicate.

Array.prototype.findLast() β€” no match

Just like find(), findLast() returns undefined when no element satisfies the predicate.

Challenge: Implement findLastNegative(nums) that returns the last negative number, or undefined if none.

findLastNegative([1, -2, 3, -4]) // β†’ -4
findLastNegative([1, 2, 3])      // β†’ undefined
πŸ’‘ Hints (2)
  • β€’ `n < 0` excludes zero β€” use `n <= 0` to include it.
  • β€’ When no element matches, `findLast()` returns `undefined`, just like `find()`.