Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

find

find() returns the first array element for which a testing callback returns a truthy value. If no element matches, it returns undefined.

How it works

Call find() with a callback that evaluates each array element. Iteration proceeds from the lowest index; the first truthy callback result immediately produces that element, while a completely unsuccessful search produces undefined.

Syntax

find(callbackFn)
find(callbackFn, thisArg)

Parameters

NameTypeDescription
callbackFnFunctionA function called for each array element. It should return a truthy value for a match and a falsy value otherwise; it receives the element, index, and array.
thisArganyOptional value used as this when callbackFn executes.

Returns

The first element satisfying the testing function; otherwise undefined.

Key points

  • The result is the matching element itself, not its position.
  • Use an explicit undefined check or optional chaining when no match is a valid outcome.
  • Elements are examined from the smallest index upward.
  • The first truthy predicate result ends the search immediately.
  • The required callback defines the condition used to identify a match.
  • The callback can inspect the current value while deciding whether it matches.

Common mistakes

  • Sparse arrays are still traversed at every index, so a hole is tested as undefined rather than skipped.
  • A sparse-array hole and an explicit undefined value are indistinguishable to the callback.
  • The method can be borrowed for array-like objects that provide length and integer-keyed properties.

Example: Find the first matching object

const users = [
  { id: 1, name: "Ada" },
  { id: 2, name: "Linus" },
];

const user = users.find(({ id }) => id === 2);
console.log(user?.name); // "Linus"
console.log(users.find(({ id }) => id === 3)); // undefined

The callback tests each user in order, and find() returns the first object whose id matches. Optional chaining safely handles the undefined result when no user matches.

When to use it

Lookup by identifier

Retrieve one record from an in-memory collection by an identifying property.

First qualifying value

Select the first item meeting a threshold, status, or validation rule.

Array-like search

Search array-like data structures using length and integer-keyed properties.

Interview questions

QWhat does Array.prototype.find() return, and when does it stop?

find() calls the predicate in ascending index order and stops as soon as the predicate returns a truthy value. It returns that element; if no predicate call succeeds, it returns undefined.

Follow-up: How does it treat sparse arrays? It invokes the callback for every index, treating empty slots like undefined.

QWhat arguments can the find() callback receive?

callbackFn receives the current element, its index, and the array being searched. An optional thisArg supplies the callback's this value when the callback is invoked.

Follow-up: What must the callback return? Any truthy value signals a match; a falsy value continues the search.

QIs find() limited to actual arrays?

find() is generic: it only requires the this value to have a length property and integer-keyed properties. Therefore, it can operate on suitable array-like objects rather than only Array instances.

Follow-up: What happens when the callback never returns truthy? The complete traversal finishes and the method returns undefined.

  • Array.prototype.findIndex()
  • Array.prototype.findLast()
  • Array.prototype.filter()
  • Array.prototype.some()

Source: Array.prototype.find() - JavaScript - MDN Web Docs. Checked 2026-10-15.