JS Built-ins
← Array69 / 226
ArrayArray.prototype.findbeginner

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

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

find() returns undefined when no element satisfies the predicate β€” check for this case.

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

When no element satisfies the predicate, find() returns undefined (not -1 like indexOf).

Challenge: Implement findEven(nums) that returns the first even number, or undefined if none exists.

findEven([1, 3, 4, 5]) // β†’ 4
findEven([1, 3, 5])    // β†’ undefined
πŸ’‘ Hints (2)
  • β€’ `find()` returns `undefined`, not `-1` β€” check carefully in conditionals.
  • β€’ To test the return value, use `=== undefined` or the optional chaining operator.