Array.prototype.find()
filter for exactly one result. It stops at the first match, and hands back the element itself rather than a one-element array.
Common call
items.find(x => x.id === id)
Returns
the element, or undefined
Replaces
filter(...)[0], which scans the whole array
Watch out
a falsy element is indistinguishable from "not found"
array.find(callback[, thisArg])
→ any
Demo
Live evaluation
Try:
Inputs
itemsnumber[]numbers, comma separated
thresholdnumberfind the first above this
Output
[1, 2, 3, 4].find(x => x > 2)
3
The ELEMENT comes back, not an array and not an index — that is the difference from filter and findIndex. Iteration stops at the first match, so later matches are never even tested. When nothing matches you get undefined, which is the value to check for; note the demo shows it plainly rather than as an empty array.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| callback | Function | yes | Called as callback(element, index, array). The first element for which it returns truthy is returned. |
| thisArg | any | no (undefined) | Value of `this` inside the callback. Ignored for arrow functions. |
Return value
any — The first element for which the callback returned truthy, or undefined when none did. Never an array.
Common patterns
Look up by id
The single most common use.
const user = users.find(u => u.id === targetId);
Guard the result
undefined is the "not found" signal.
const match = items.find(pred); if (!match) throw new Error("not found");
Default with nullish coalescing
Falls back only on undefined, not on a falsy match.
const found = items.find(pred) ?? fallback;
Examples
1. First match
[1, 2, 3, 4].find(x => x > 2)
Returns
32. No match
[1, 2, 3].find(x => x > 99)
Returns
undefined3. Empty array
[].find(x => true)
Returns
undefined4. Element, not array
[1, 2, 3].find(x => x === 2)
Returns
2 // not [2]5. Falsy match
[0, 1].find(x => x === 0)
Returns
06. Index instead
[1, 2, 3].findIndex(x => x > 99)
Returns
-1Pitfalls
1. A falsy match looks like no match
find returns 0, empty string or false exactly as it returns undefined for failure — so a truthiness test cannot tell them apart. This bites whenever the array holds numbers that can be zero.
Zero treated as missing
const n = [0, 1].find(x => x === 0); if (!n) { /* "not found" */ }
wrongly takes the not-found branch
Compare to undefined
if (n === undefined) { ... }
correct
2. It returns the element, not an array
Coming from filter, it is easy to index the result. Doing so on an object gives undefined, and on a number throws nothing but produces nonsense.
Indexing the element
users.find(u => u.id === 1)[0]
undefined
Use it directly
users.find(u => u.id === 1)
the user object
3. filter(...)[0] does more work
filter scans the whole array and allocates a result array, then you throw all but one element away. find stops at the first hit and allocates nothing.
Scans everything
items.filter(pred)[0]
O(n) always, plus an array
Stops early
items.find(pred)
stops at the first match
4. Using || for a default swallows falsy matches
A found value of 0 or empty string is falsy, so || replaces a legitimate result with the fallback. ?? only triggers on null and undefined.
Falsy replaced
[0, 1].find(x => x === 0) || 99
99
Nullish coalescing
[0, 1].find(x => x === 0) ?? 99
0
When to use
Use it
- Looking up a single record by id or key
- Any search where you expect at most one result
- Short-circuiting on the first match in a large array
Reach for something else
- You want every match → filter
- You want the position → findIndex
- You only need a yes/no answer → some
- Matching a plain value rather than a predicate → includes
Notes
Complexity
O(n) worst case; stops at the first match
Return
The element or undefined; the array is never modified
CPython impl
V8: Builtins-array-find.tq
Memory
No allocation — unlike filter, which builds an array
Thread-safe
Single-threaded; mutating the source inside the callback is undefined behaviour for unvisited indices
FAQ
Compare against undefined explicitly, or use findIndex and check for -1. A plain truthiness test cannot distinguish them, because find returns the element as-is — including 0 and empty string.
const i = items.findIndex(pred); if (i !== -1) { const match = items[i]; }
History
ES2015
find and findIndex added, alongside the rest of the iteration helpers.
ES2023
findLast and findLastIndex added for searching from the end.