Leaderboard
Javascript Jun 25, 2026
Javascript Array Find Vs Filter First Match

JavaScript Array.find() — What is the output?

This is a daily Javascript challenge from the CodeShot archive. Practice your knowledge of Array Find Vs Filter First Match and improve your technical interview readiness.

const arr = [1, 2, 3, 4, 5]
const result = arr.find(n => n > 3)
console.log(result)
A [4, 5]
B 4
C true
D undefined

Detailed Explanation

Why This Question Matters

If you're just starting out with JavaScript, you'll see .find() everywhere. It looks simple, but there's a common point of confusion for juniors: the difference between finding a *value* and finding a *list of values*.

A lot of developers instinctively think in terms of "filtering." When they see a condition like n > 3, their brain immediately jumps to "give me everything that matches this." If you make that assumption here, you'll pick the wrong answer. Understanding exactly how .find() behaves—and how it differs from .filter()—is a fundamental step in mastering array methods.

Understanding the Code

Let's look at the snippet:

const arr = [1, 2, 3, 4, 5]
const result = arr.find(n => n > 3)
console.log(result)

Here is what's happening under the hood.

First, we have an array of numbers. Then, we call the .find() method. This method takes a callback function—in this case, an arrow function n => n > 3—and executes it for every element in the array, one by one.

1. It checks 1: Is 1 > 3? False. Move to the next.
2. It checks 2: Is 2 > 3? False. Move to the next.
3. It checks 3: Is 3 > 3? False. Move to the next.
4. It checks 4: Is 4 > 3? True.

The moment .find() hits a value that returns true, it stops everything. It doesn't care if there are more elements in the array (like 5 in this case). It just grabs that specific element and returns it immediately.

Finding the Correct Answer

The correct answer is 4.

Why not [4, 5]? Because that's what .filter() does. If the code had used arr.filter(n => n > 3), the result would be a new array containing every element that passed the test.

But .find() is designed to return a single element. It returns the *first* element in the array that satisfies the provided testing function. Since 4 is the first number it encounters that is greater than 3, it returns 4 and exits.

If no elements had matched the condition (for example, if we searched for n > 10), .find() would return undefined.

Common Mistakes Developers Make

The biggest mistake is confusing .find() with .filter(). I see this in code reviews all the time. A developer wants a single object from a list (like a user by their ID) but uses .filter(). This results in an array with one item [user], and then they spend twenty minutes wondering why their code is breaking when they try to access user.name (because they're actually calling .name on an array, not an object).

Another edge case is the "empty" result. Since .find() returns undefined when nothing is found, you have to be careful. If you try to access a property on the result of a .find() call without checking if it exists, your app will crash with the classic TypeError: Cannot read property '...' of undefined.

Always remember:
- .filter() $\rightarrow$ always returns an array (even if it's empty).
- .find() $\rightarrow$ returns the element itself or undefined.

Real-World Usage

In a real production app, you rarely use .find() on a simple array of numbers. You'll mostly use it to pluck a specific object out of a state array or an API response.

Imagine you have a list of products in an e-commerce store and you have the productId from the URL. You don't want a list of products; you want the one specific product that matches that ID.

const products = [
  { id: 'p1', name: 'Mechanical Keyboard' },
  { id: 'p2', name: 'Gaming Mouse' },
  { id: 'p3', name: 'Ultrawide Monitor' }
];

const productId = 'p2';
const product = products.find(p => p.id === productId);

console.log(product.name); // "Gaming Mouse"

This is the most efficient way to handle this because .find() stops iterating the moment it finds the match. If you have a list of 10,000 items and the match is at index 5, .find() stops at 5. .filter() would keep checking the other 9,995 items even after it found what it needed.

Key Takeaways

- Use .find() when you need one specific item and you want the first match.
- Use .filter() when you need all items that match a condition.
- .find() returns the value of the element, not an array.
- If no match is found, .find() returns undefined.
- Performance-wise, .find() is faster for single searches because it short-circuits the loop.

Why this matters

Understanding Array Find Vs Filter First Match is crucial for passing technical interviews. In real-world applications, this concept often leads to subtle bugs if not handled correctly. For more details, you can always refer to the official MDN Documentation.

📝
Reviewed by CodeShot Editorial
Every challenge is code-reviewed by senior developers to ensure accuracy and real-world relevance. Learn more.

Ready for your shot?

Join thousands of developers solving one logic puzzle every morning.

Solve Today's Challenge →