JavaScript filter() vs find(): Return All Matches or One?

Learn the difference between JavaScript filter and find, what each method returns, and how to choose the right one for product and data searches.

JavaScript filter() vs find(): Return All Matches or One?

filter() vs. find(): Return All Matches or Just One?

You have a list of products and want to locate an item. Should you use filter() or find()? The answer depends on how many matches you need: filter() returns every item that matches; find() returns the first matching item.

Both methods leave the original array unchanged by themselves. You can run these examples in a modern browser console or recent Node.js.

filter() returns a new array of matches

filter() calls a test function for each item and creates a new array containing the items for which the callback returns a truthy value:

const products = [
  { id: 1, name: "Notebook", inStock: true },
  { id: 2, name: "Pen", inStock: false },
  { id: 3, name: "Desk lamp", inStock: true },
];

const availableProducts = products.filter((product) => product.inStock);

console.log(availableProducts);
// Notebook and Desk lamp
console.log(availableProducts.length); // 2

Use filter() when you want all matching items—for example, every product that is in stock or every message from a particular sender. If no items match, it returns an empty array.

find() returns the first match

find() uses a similar test callback, but returns the first matching item instead of an array. If no item matches, it returns undefined:

const product = products.find((item) => item.id === 2);

console.log(product); // { id: 2, name: "Pen", inStock: false }

const missingProduct = products.find((item) => item.id === 99);
console.log(missingProduct); // undefined

Use find() when you need one item, such as locating a product by ID. It stops checking once it finds the first match. If there is no match—or the match is near the end—it may still check every item.

Compare their results

MethodReturnsWhen to use it
filter()A new array containing all matching items; possibly []You need every match
find()The first matching item; otherwise undefinedYou need one match

Both callbacks receive the current item, its index, and the original array. Neither method mutates the array on its own, though the callback can still mutate objects or external state. Also, filter() creates a new array, but it is shallow: matching object values are the same object references as in the original array.

A common mistake: expecting filter() to return one object

Even if exactly one item matches, filter() still returns an array:

const matches = products.filter((item) => item.id === 2);
console.log(matches[0]); // The first item in the result array

If you want one item directly, use find():

const match = products.find((item) => item.id === 2);
console.log(match); // The product object itself

If you need the index of the first match instead of the item, use findIndex(). It returns -1 when there is no match.

Common beginner mistakes

Using find() when every match is needed

find() stops at the first match. Use filter() if you need all matching values.

Forgetting the “no match” result

filter() returns [] when nothing matches. find() returns undefined. Check for that before reading a property from the result:

const product = products.find((item) => item.id === 99);

if (product) {
  console.log(product.name);
} else {
  console.log("Product not found");
}

Assuming find() is always much faster

find() can stop early, which is useful when the first match is near the beginning. If the match is near the end or doesn't exist, it still needs to inspect the whole array. Choose primarily by whether you need one result or all results.

Practice

Using the products array above, use filter() to get all products in stock and find() to get the product with ID 3. Then search for an ID that isn't present and handle the undefined result.

What we learned

  • filter() returns a new array with every item that passes its test.
  • find() returns the first item that passes its test, or undefined if there isn't one.
  • find() can stop early; filter() checks the full array to collect all matches.
  • Both methods leave the original array unchanged by themselves, but callbacks can have side effects.
  • Use findIndex() when you need the position of the first match.

What's next?

Next, we'll explore what reduce() is used for and how it combines array items into a single result.

Conclusion

Choose based on the result you need: filter() for a collection of matches, find() for the first matching item. That also makes your code easier to understand at a glance. Remember to handle the empty-array or undefined case when no item matches.

Written by

Dev Likhon

Dev Likhon

Full Stack Developer & Tech Educator

6+ years of professional development experience.

About Me
Share:

Comments

Share what you learned or ask a question.

Loading comments...