JavaScript Reference
Array.prototype.find()
Returns the first element that matches a test. Short-circuits on the first hit.
Last updated: October 2026
Definition: Array.prototype.find() returns the first element in the array that passes the provided test function. If no element matches, it returns undefined. It stops iterating as soon as a match is found — making it fast for large arrays when you only need one result.
🎬 Live Example
Five common find patterns. Click any button to see the output.
💡 find returns the element, not an array. If nothing matches, it returns undefined.
Basic Syntax
array.find(callback(element, index, array))
// Returns the first matching element, or undefined
The callback returns truthy to match, falsy to skip.
🎯 Syntax & Parameters
| Parameter | Type | Description |
|---|---|---|
callback | Function | Runs on each element until it returns truthy. |
element | any | The current element being tested. |
index | number | Index of the current element. |
array | Array | The original array. |
Find Object by Property
const users = [
{ id: 1, name: 'Alice', email: 'alice@example.com' },
{ id: 2, name: 'Bob', email: 'bob@example.com' },
{ id: 3, name: 'Carol', email: 'carol@example.com' }
];
const user = users.find(u => u.id === 2);
// { id: 2, name: 'Bob', email: 'bob@example.com' }
const byEmail = users.find(u => u.email === 'carol@example.com');
// { id: 3, name: 'Carol', email: 'carol@example.com' }
This is the most common use case — looking up an entity by ID or unique property.
Handle "Not Found"
const user = users.find(u => u.id === 99);
if (!user) {
console.log('User not found');
}
// Or with optional chaining for nested access
const email = users.find(u => u.id === 99)?.email;
// undefined (no error)
Always check for undefined. find returns undefined when nothing matches. Accessing properties on undefined throws a TypeError.
Using the Index Argument
const numbers = [10, 20, 30, 40, 50];
// Find first number at an even index that's > 20
const result = numbers.find((n, i) => i % 2 === 0 && n > 20);
// 30 (index 2)
find + Update Pattern
const products = [
{ id: 1, name: 'Widget', inStock: true },
{ id: 2, name: 'Gadget', inStock: false }
];
// Find then update
const product = products.find(p => p.id === 2);
if (product) {
product.inStock = true;
}
// Note: this mutates the original object.
For immutable updates, use map instead:
const updated = products.map(p =>
p.id === 2 ? { ...p, inStock: true } : p
);
Real-World: Find in API Response
const response = {
users: [
{ id: 101, name: 'Alice', role: 'admin' },
{ id: 102, name: 'Bob', role: 'user' }
]
};
// Get the admin user
const admin = response.users.find(u => u.role === 'admin');
// Safe access to avoid undefined errors
const adminName = response.users.find(u => u.role === 'admin')?.name;
// 'Alice'
🎯 find vs filter vs findIndex vs some
| Method | Returns | Use when |
|---|---|---|
find | First matching element or undefined | You need one specific element |
filter | Array of all matching elements | You need all matches |
findIndex | Index of first match or -1 | You need the position |
some | Boolean | You only need to know if any match |
every | Boolean | You need all to match |
⚠️ Common Mistakes
- Forgetting find returns undefined. Always check the result before using it. Use optional chaining or an if check.
- Using find when you need all matches. Use
filterfor that. - Confusing find with filter. find returns a single element. filter returns an array (even for one match).
- Mutating the returned object. Objects are shared by reference. Mutating them mutates the source.
- Expecting find to return a new array. It doesn't — it returns the actual element.
- Not short-circuiting mentally. find stops at the first match — good for performance but be aware that later duplicates are ignored.
- Using find on empty arrays. Safe — returns undefined. No error thrown.
❓ Frequently Asked Questions
What does Array.find() do in JavaScript?
Array.find() returns the first element in the array that satisfies a provided test function. If no element matches, it returns undefined. It does not modify the original array and short-circuits as soon as a match is found.
What is the difference between find and filter?
find returns the first matching element (or undefined). filter returns a new array with all matching elements. Use find when you need a single result, filter when you need all matches.
What is the difference between find and findIndex?
find returns the matching element itself. findIndex returns the index of the matching element. Both short-circuit at the first match and return undefined or -1 respectively when no match is found.
Does find modify the original array?
No. find does not modify the array. It only reads. If you mutate the returned object, though, you'll mutate the original element because objects are shared by reference.
Is find faster than filter?
Yes, generally. find short-circuits and stops iterating as soon as it finds a match. filter must check every element. For large arrays where you only need one result, find is significantly faster.