JavaScript Array find Method and When It Returns undefined
Array find() returns the first item that passes a test, or undefined when none does. See why that undefined is ambiguous and how find differs from filter.

users.find((user) => user.id === id) is the most common line in any JavaScript codebase that holds a list in memory, and its return value is the most common source of Cannot read properties of undefined. The method is simple: return the first match or undefined. The trouble is everything that follows when the match is not there. This post covers the behavior on Node.js 24, the ambiguity in its return value, and the point at which a Map becomes the better lookup. The surrounding language features are laid out in the complete guide to modern JavaScript.
The behavior in one example:
const inventory = [
{ sku: "A-100", stock: 4 },
{ sku: "B-200", stock: 0 },
{ sku: "C-300", stock: 12 },
];
const empty = inventory.find((item) => item.stock === 0);
const missing = inventory.find((item) => item.sku === "Z-999");
console.log(empty);
console.log(missing);{ sku: 'B-200', stock: 0 }
undefinedWhen nothing matches, the result is undefined, with no error and no hint about why. The callback receives (value, index, array), the same as the other iteration methods.
find stops, filter does not
The reason to prefer find() over filter(...)[0] is work, not style. We can count it.
const ids = [101, 102, 103, 104, 105];
let findCalls = 0;
ids.find((id) => {
findCalls += 1;
return id === 102;
});
let filterCalls = 0;
ids.filter((id) => {
filterCalls += 1;
return id === 102;
})[0];
console.log(findCalls, filterCalls);2 5find() ran its callback twice and quit. filter() ran it five times, built an array and then indexed into it. For five items, nothing. For a list of fifty thousand records searched on every request, the early exit is the whole point. When the match is usually near the end, the two cost about the same, which is why we do not claim a general speed-up.
It returns the same object, not a copy
Like filter(), find() hands back the reference stored in the array.
const settings = [
{ key: "theme", value: "light" },
{ key: "locale", value: "en-US" },
];
const theme = settings.find((setting) => setting.key === "theme");
theme.value = "dark";
console.log(settings[0].value);darkThat is the behavior you want for "find this record and update it", and the one to remember when you pass the found item to code that might edit it. For primitives, find() returns the value, and there is no reference to share.
The ambiguity in undefined
If an array can legitimately hold undefined, then find() returning undefined is two different answers: "found an item that is undefined" and "found nothing". The same applies to null, 0, "" and false as search targets, because the callback result must be truthy for a match, so searching for a falsy value by value needs a strict comparison, not the value itself.
const readings = [3, undefined, 8];
const hit = readings.find((value) => value === undefined);
const index = readings.findIndex((value) => value === undefined);
console.log(hit);
console.log(index);undefined
1hit is undefined whether the search succeeded or not. findIndex() returns 1, and -1 would mean no match, so the answer is unambiguous. The findIndex() post covers the -1 convention and the check that goes with it. We reach for findIndex() whenever the element type includes undefined, and find() the rest of the time.
A second trap is the callback itself returning a non-boolean. find((item) => item.discount) matches the first item with a non-zero, non-empty discount, and skips one whose discount is 0 even though it exists. Compare against something explicit instead.
What the callback should return
The callback's result is converted to a boolean, so any truthy value counts as a match. That leniency produces a quiet class of bug: find((item) => item.quantity) skips an item whose quantity is 0, even though the item exists and the caller wanted it. Write the comparison out. item.quantity === 0 and item.quantity > 0 each say what they mean, and neither depends on how 0 converts.
A second habit is to keep the predicate a pure question. A callback that increments a counter or mutates the item will run a number of times that depends on where the match sits, which makes the effect unpredictable. The question should be answerable from the item alone.
Holes are visited
Most iteration methods skip holes, and find() does not. Every index from 0 to length - 1 is passed to the callback, with undefined for a missing one.
const sparse = [1, , 3];
const visited = [];
sparse.find((value, index) => {
visited.push(index);
return false;
});
console.log(visited);[ 0, 1, 2 ]The hole at index 1 was tested. The same is true of findIndex(), findLast() and findLastIndex(), which were added with that behavior. The older every(), some() and filter() skip holes, so a check that mixes them on a sparse array can disagree with itself.
Turning find results into safe code
Because undefined is the failure value, the usual follow-up is optional chaining with a fallback.
const users = [{ id: 1, name: "Ada" }];
const label = users.find((user) => user.id === 7)?.name ?? "unknown";
console.log(label);unknownThat works when a default is acceptable. When a missing record is a bug, a helper that throws gives a better error than a TypeError three lines later.
function findOrThrow(items, predicate, message) {
const item = items.find(predicate);
if (item === undefined) {
throw new Error(message);
}
return item;
}
try {
findOrThrow([{ id: 1 }], (row) => row.id === 2, "no row with id 2");
} catch (error) {
console.log(error.message);
}no row with id 2In TypeScript the return type of find() is T | undefined, so the compiler forces the check. A type predicate callback narrows the result, for example find((x): x is Admin => x.role === "admin") returns Admin | undefined. The helper above returns T once written with a generic, which keeps callers clean.
An async callback matches the first item
As with the other callback methods, an async predicate returns a promise, and a promise is truthy, so the first item always wins.
const isReady = async (job) => job.ready;
const jobs = [{ ready: false }, { ready: true }];
console.log(jobs.find(isReady));{ ready: false }The first job is not ready, and it was returned anyway. Resolve the promises first, as shown in the filter() post, or use a loop with await when you want to stop at the first success.
Lookups in a loop need a Map
A single find() is fine. A find() inside a loop over another list is a nested loop, and its work multiplies. Matching ten thousand orders to ten thousand customers this way means up to a hundred million comparisons. Build an index once and look up in constant time.
const customers = [
{ id: 1, name: "Ada" },
{ id: 2, name: "Grace" },
];
const orders = [{ customerId: 2 }, { customerId: 1 }, { customerId: 2 }];
const byId = new Map(customers.map((customer) => [customer.id, customer]));
console.log(orders.map((order) => byId.get(order.customerId)?.name));[ 'Grace', 'Ada', 'Grace' ]The Map costs one pass to build and some memory, and pays back as soon as there are more than a handful of lookups. Sets and Maps versus objects and arrays covers when each structure is the right container.
Pinning the not-found path with a test
The success path of a lookup gets exercised by every manual run. The miss is where regressions hide, because it needs a deliberately wrong id. A small test fixes the contract for both.
import { test } from "node:test";
import assert from "node:assert/strict";
const users = [
{ id: 1, name: "Ada" },
{ id: 2, name: "Grace" },
];
const nameFor = (id) => users.find((user) => user.id === id)?.name ?? "unknown";
test("returns the name when the id exists", () => {
assert.equal(nameFor(2), "Grace");
});
test("falls back when the id is missing", () => {
assert.equal(nameFor(99), "unknown");
});
test("does not match on a string id", () => {
assert.equal(nameFor("2"), "unknown");
});Run it with node --test on Node.js 24. The third test documents a real production bug: ids from a URL are strings, ids in the data are numbers, and === refuses the match. Coercing at the boundary, with Number(params.id), is better than loosening the comparison inside every lookup.
A related habit saves a debugging session. When a find() that "should work" returns undefined, log typeof of both sides of the comparison before anything else. Mismatched types account for most of the misses we have traced.
When find is the wrong tool
Use includes() when you only need to know whether a primitive is present, because it returns a boolean and has no ambiguity. Use some() when you need a yes or no for a condition, as it returns true or false and never confuses a stored undefined with a miss. Use filter() when more than one match matters.
And if the thing you are searching is sorted, a binary search beats any of these, though you rarely need one below a few thousand items.
The method is specified in Array.prototype.find, and the MDN page for find documents the treatment of empty slots.


