Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › JavaScript › Reference › find()

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.

Input:

                
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
callbackFunctionRuns on each element until it returns truthy.
elementanyThe current element being tested.
indexnumberIndex of the current element.
arrayArrayThe original array.
1

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.

2

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.

3

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)
4

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
);
5

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
findFirst matching element or undefinedYou need one specific element
filterArray of all matching elementsYou need all matches
findIndexIndex of first match or -1You need the position
someBooleanYou only need to know if any match
everyBooleanYou need all to match

⚠️ Common Mistakes

❓ 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.

🎯 What's Next?

← All JavaScript Reference