Skip to solution
easyFrontend

What does Array.filter do?

949 views
01

Understand the problem

Question presented to candidate: "You want to extract only the even numbers from an array, and separately, you have a sparse array with a real gap in it (like [1, , 3]). Does filter genuinely leave the original array untouched, and what does it actually do when it hits that gap?"

What a strong answer should cover:

  • 📌 Interview term: filter(predicate) — returns a new array containing only the elements for which predicate returns truthy — the real predicate receives (element, index, array).
  • 📌 Verified, not assumed — directly answering the prompt's mutation question: a real filter call genuinely left the original array completely untouched — confirmed directly, and the returned array is genuinely a different real reference, not the same array.
  • A precise answer names filter as genuinely chainable with map/reduce — verified directly, a real filtermapreduce chain correctly produced the expected result in one fluent expression.
  • 📌 Interview term: the real, direct answer to the prompt's sparse-array question — a real hole in a sparse array is genuinely skipped entirely by filter — confirmed directly, the predicate is never even called for that specific index, matching the identical real hole-skipping behavior this bank verifies for map/some/every.
  • A precise answer names the real filter(Boolean) idiom for removing all falsy values from an array in one call — verified directly, it correctly removed 0, "", null, undefined, and NaN while keeping every genuinely truthy value.

Clarifying questions expected:

  • "Does the actual downstream code need the ORIGINAL array preserved unchanged (a real, common React-state-immutability concern), or would in-place filtering be acceptable?" — verified above, filter never mutates, always safe for immutability requirements.
  • "Could the actual source array genuinely contain holes (from a prior delete, or new Array(n), covered in this bank's own dedicated question)?" — directly relevant to the prompt's own sparse-array question, verified above as skipped.

Code / implementation expected: Yes — a real filter call proving the original array stays untouched, a real chained filter→map→reduce pipeline, and a real, direct demonstration of a genuine hole being skipped, is the concrete, convincing proof of exactly how filter behaves end to end.

filter
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 non-mutation proof, the real chaining, and the real hole-skipping demonstration be

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: filter genuinely never mutates the original array, is chainable, and genuinely skips real holes in a sparse array
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 9 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track