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

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

πŸ“– Usage Example
const nums = [3, 7, 12, 5]
nums.find(n => n > 10)  // β†’ 12  (first match)

Use find() to get the first element that satisfies a condition, or undefined if none does.

Array.prototype.find()

Array.prototype.find(callback) returns the first element for which the callback returns a truthy value, or undefined if no element matches. It short-circuits on the first match.

Challenge: Implement findFirst(nums, min) that returns the first number >= min.

findFirst([1, 5, 3, 10], 5) // β†’ 5
findFirst([1, 2, 3], 10)    // β†’ undefined
πŸ’‘ Hints (2)
  • β€’ `find()` returns the element itself, not its index.
  • β€’ `find()` stops at the first match β€” it does not scan the entire array unnecessarily.