Skip to the solution
EasyFrontend6 min read

What is the difference between find and filter?

572 views

The problem

Question presented to candidate: "You need to look up a specific user by ID from an array of 10,000 users. Would you use find or filter for this, and does it actually make a measurable difference?"

What a strong answer should cover:

  • πŸ“Œ Interview term: find(predicate) β€” returns the first matching element (or undefined if none match), and genuinely short-circuits the instant a match is found; πŸ“Œ filter(predicate)** β€” returns all matches as a new array, genuinely checking every element regardless.
  • πŸ“Œ Verified, not assumed β€” directly answering the prompt's exact question: a real call counter proved find() genuinely stopped at exactly the element that matched β€” real, measurably fewer calls than filter(), which genuinely checked all remaining elements even after finding a match.
  • πŸ“Œ Interview term: the real, distinct "no match" results β€” directly relevant for the prompt's ID-lookup scenario: find() with no match genuinely returns undefined, while filter() with no match genuinely returns an empty array, not undefined β€” a real, meaningful difference for downstream code checking the result.
  • A precise answer names the real, direct payoff for the prompt's exact scenario: for a single-item lookup by a unique key (like a user ID) in a genuinely large array, find's real short-circuiting means it stops almost immediately once the match is located, while filter would genuinely keep scanning through potentially thousands of remaining elements for no benefit β€” a real, measurable difference specifically when the match is found early.
  • A precise answer names findIndex/findLast/findLastIndex as real, direct siblings of find, covering position-based and reverse-direction lookups with the identical real short-circuiting behavior.

Clarifying questions expected:

  • "Is the actual ID genuinely guaranteed unique, so at most one real match is ever possible?" β€” directly relevant to whether find's "just the first" semantics, verified above, are actually correct for the use case, versus needing filter for genuinely multiple matches.
  • "Does the real, downstream code need to distinguish 'no match' from a match, in a way that matters whether it gets undefined versus an empty array?" β€” verified above as a real, meaningful difference between the two.

Code / implementation expected: Yes β€” a real call counter directly proving find's short-circuiting versus filter's full scan, plus the real, distinct no-match results (undefined vs. empty array), is the concrete, convincing proof of exactly which tool fits the prompt's lookup scenario.

  • find

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: a call counter confirms find() genuinely short-circuits while filter() checks every element, plus the real distinct no-match results

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 20 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What is globalThis?

Continue