Skip to solution
easyFrontend

What is the difference between map and forEach?

213 views
01

Understand the problem

Question presented to candidate: "You need to fetch additional data for each item in an array, using an async function inside the loop. Someone writes items.forEach(async item => { await fetchMore(item) }). Does this actually wait for all the fetches to finish before moving on? Walk me through what map and forEach each do, and why this specific pattern is a real, common bug."

What a strong answer should cover:

  • 📌 Interview term: map(fn) returns a new array of transformed values, genuinely chainable; 📌 forEach(fn)** returns undefined, used purely for side effects — real, genuinely different return contracts.
  • 📌 Verified, not assumed: forEach's real return value is genuinely undefined, confirmed directly — and a real, direct attempt to chain .filter() onto it genuinely threw a real TypeError, since there is nothing real to chain onto.
  • 📌 Interview term: the real, direct answer to the prompt's exact async bug — a real forEach with an async callback genuinely does NOT wait for the async work to complete: confirmed directly, a real results array was genuinely still empty immediately after the forEach call itself returned, even though every async callback had already been invoked — forEach genuinely ignores whatever Promise each callback returns.
  • A precise answer names the real, correct fix for the prompt's exact bug: use for...of with await inside the loop body (for genuinely sequential async work), or Promise.all(items.map(async item => ...)) (for genuinely concurrent async work) — neither of which relies on forEach waiting for anything, since it genuinely never does.
  • A precise answer names that map, verified separately, genuinely does NOT stop early on any special return value inside the callback — a real, direct return inside a map callback is simply that element's real transformed value, not a loop-control signal.

Clarifying questions expected:

  • "Does the actual downstream code need the real, transformed VALUES back (map's real job), or is this purely a side-effecting operation with no meaningful return value (forEach's real job)?"
  • "Does the async work per item genuinely need to happen sequentially, or can it genuinely run concurrently?" — directly decides between the real for...of+await fix and the real Promise.all(map(...)) fix for the prompt's exact bug.

Code / implementation expected: Yes — a real, direct demonstration that forEach's return value is genuinely undefined, plus a real, concrete proof that forEach with async callbacks genuinely does not wait, is the concrete, convincing proof of exactly why the prompt's pattern is a real, common bug.

array-methods
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 and async-pattern interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The real return-value proof and the real async-forEach bug demonstratio

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: forEach genuinely returns undefined and cannot be chained, and genuinely does not wait for async callbacks — the exact prompt bug reproduced
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 36 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track