search

Array.prototype.find

Returns the first element for which the callback returns truthy, or undefined if none match. Reach for it when you need the matching item itself, not just its index.

arr.find(callbackFn[, thisArg])

Parameters

Parameter Purpose
callbackFn(element, index, array) Predicate; first element returning truthy wins
thisArg Value used as `this` inside callbackFn

Examples

console.log([1,2,3,4].find(n => n > 2));

Logs: 3

console.log([1,2,3].find(n => n > 99));

Logs: undefined

const users = [{id:1},{id:2},{id:3}]; console.log(users.find(u => u.id === 2));

Logs: {id: 2}

console.log([0,'',null].find(v => v !== null));

Logs: 0 (find returns the value even if it is falsy)

Gotcha

Returns undefined when nothing matches — check `=== undefined`, not truthiness (0 and '' are valid matches). Short-circuits on the first hit.

Related methods

← All JS array methods · TypeScript vs JavaScript