Array.find()
Returns the first element that matches a predicate, or undefined.
array.find(predicateFn, thisArg?)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Returns the first element that matches a predicate, or undefined.
The find() method executes a provided predicateFn once for each index of the array until it finds one where the function returns a truthy value. If such an element is found, find() immediately returns the value of that element; otherwise, it returns undefined. Because it short-circuits upon finding a match, it is more performant than Array.prototype.filter() when searching for a single unique record. It iterates over all indices, including 'holes' in sparse arrays (treating them as undefined), which distinguishes it from some older ES5 methods. Note that find() does not mutate the array on which it is called, but the predicateFn can. For performance-critical applications with large datasets, remember that the worst-case complexity is O(N). Internet Explorer (IE) is not supported.
Quick reference
Syntax
array.find(predicateFn, thisArg?)
Inputs
Parameters
See it in practice
Examples
Finding an object by property
const inventory = [
{ name: 'apples', quantity: 2 },
{ name: 'bananas', quantity: 0 },
{ name: 'cherries', quantity: 5 }
];
const result = inventory.find(function(item) {
return item.name === 'cherries';
});
console.log(result);{ name: 'cherries', quantity: 5 }
A common use case for finding a specific object within a collection based on a unique key.
Short-circuiting on first match
const numbers = [5, 12, 8, 130, 44];
const found = numbers.find(function(element) {
return element > 10;
});
console.log(found);12
Even though 130 and 44 also satisfy the condition, find() stops as soon as it encounters 12.
Handling missing elements
const scores = [10, 20, 30];
const highscore = scores.find(function(s) {
return s > 100;
});
console.log(highscore);undefined
When no element satisfies the predicate, the method returns undefined rather than throwing an error.
Debug faster
Common Errors
TypeError
Cause: Attempting to access a property on the result of find() when no match was found.
Fix: Check if the result is defined before accessing properties, or use optional chaining if the environment supports it.
const user = users.find(u => u.id === 999);
// Error: Cannot read property 'name' of undefined
console.log(user.name);LogicError
Cause: Confusing find() with filter() and expecting an array result.
Fix: Use filter() if you need all matching elements in an array, or find() if you only need the first single element.
const matches = [1, 2, 3].find(n => n > 0);
// matches is 1, not [1, 2, 3]
console.log(matches.length); // undefinedRuntime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Returns the first element that matches a predicate, or undefined.
predicate: Returns true for the desired element. thisArg: Value used as 'this' inside predicate.
TypeError: Check if the result is defined before accessing properties, or use optional chaining if the environment supports it. LogicError: Use filter() if you need all matching elements in an array, or find() if you only need the first single element.