Skip to solution
easyFrontend

What is the difference between find and filter?

572 views
01

Understand 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
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript array-method interviews. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The real call-counter proof and the real no-match comparison below were actually run

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

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

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 20 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track