Interview Prep

The questions that actually come up, in three tiers: concept questions, output-prediction puzzles, and implement-from-scratch challenges. Answer out loud before opening any solution.

Tier 1 โ€” Concept Questions

Each module page has its own question bank (1, 2, 3, 4). These are the cross-cutting ones interviewers reach for:

Explain the event loop to a junior developer.

Model answer: "JavaScript has one call stack. Slow operations are handed off to the runtime (browser/Node), which signals completion by queueing callbacks. When the stack empties, the event loop first drains ALL microtasks (Promise callbacks), then runs ONE macrotask (timer/I/O callback), then drains microtasks again, and so on. So Promise callbacks always beat setTimeout, and nothing runs until synchronous code finishes."

Callbacks vs Promises vs async/await โ€” why did the language evolve?

Callbacks nested deeply ("pyramid of doom") and split error handling across every level. Promises flattened chains and unified errors into .catch, plus enabled composition (all, race). async/await kept Promise semantics but restored straight-line readability, normal try/catch, and loops/conditionals around async calls. Key: async/await IS Promises โ€” just nicer syntax.

What is hoisting?

Declarations are processed before code runs. function declarations hoist fully (callable before their line). var hoists with value undefined. let/const hoist but stay in the "temporal dead zone" โ€” accessing them before the declaration line throws a ReferenceError.

How would you handle errors in a large async codebase?

Talking points: fail-fast with meaningful Error subclasses; try/catch only where you can add context or recover; central error middleware (Express) or a top-level handler; never swallow errors in empty catch blocks; always await or .catch fire-and-forget tasks; use allSettled for batch tolerance; log with context (request IDs).

What is an IIFE and why did people use it? What replaced it?

(function(){ ... })() โ€” an immediately-invoked function creating a private scope in the pre-ES6 era, avoiding global leaks. Block scoping (let/const) and ES modules (file-private scope by default) replaced most uses. You'll still see (async () => { ... })() to use await in non-module scripts.

Tier 2 โ€” Output Prediction Puzzles

Write your answer down before revealing. Being able to justify each line is the actual skill.

Puzzle 1 Loop + closure classic
for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log("var:", i), 0);
}
for (let j = 0; j < 3; j++) {
  setTimeout(() => console.log("let:", j), 0);
}
Answer

var: 3 ร—3, then let: 0, 1, 2. All var callbacks close over ONE shared i (already 3 when timers fire). let creates a fresh binding per iteration, so each closure captures its own value.

Puzzle 2 Micro vs macro ordering
setTimeout(() => console.log("timeout"), 0);

Promise.resolve()
  .then(() => console.log("p1"))
  .then(() => console.log("p2"));

queueMicrotask(() => console.log("qm"));

console.log("sync");
Answer

sync, p1, qm, p2, timeout. Sync first. Microtask queue at that moment: [p1, qm]. Running p1 enqueues p2 behind qm. Only after all microtasks drain does the macrotask (timeout) run.

Puzzle 3 async function boundaries
async function a() {
  console.log("a start");
  await b();
  console.log("a end");
}
async function b() {
  console.log("b");
}
console.log("script start");
a();
console.log("script end");
Answer

script start, a start, b, script end, a end. a() runs synchronously through b() (which also completes synchronously) until the await suspends it. The rest of the script runs; then the microtask resumes a.

Puzzle 4 The forgotten await
const wait = ms => new Promise(r => setTimeout(r, ms));

async function task(name, ms) {
  await wait(ms);
  console.log(name);
  return name;
}

async function main() {
  const r1 = task("A", 300);
  const r2 = task("B", 100);
  console.log(await r2, await r1);
}
main();
Answer

Logs B (at ~100ms), A (at ~300ms), then B A. Both tasks start immediately (no await on the calls), so they run in parallel. await r2 resolves first at 100ms, but the final console.log needs both, so it prints at ~300ms. Total ~300ms, not 400ms.

Puzzle 5 Rejection routing
Promise.reject(new Error("boom"))
  .then(() => console.log("then 1"))
  .catch(err => {
    console.log("caught:", err.message);
    return "recovered";
  })
  .then(v => console.log("then 2:", v))
  .catch(() => console.log("catch 2"));
Answer

caught: boom, then then 2: recovered. The rejection skips then 1, lands in the first catch, which returns a value โ€” making the chain fulfilled again. then 2 receives "recovered". catch 2 never fires.

Tier 3 โ€” Implement-From-Scratch Challenges

The most common "write code on the spot" asks. Solutions live in the module exercises โ€” do them from a blank file, timed (15 min each).

ChallengeTests yourโ€ฆSolution in
once(fn) / memoize(fn)closuresModule 1, ex. 2 & 4
debounce(fn, ms)closures + timerstry it โ€” like once() with a resettable timer
wait(ms) / sleepPromise constructionModule 3, ex. 2
retry(fn, n) with backoffasync control flowModule 4, ex. 4
promiseAll(promises)Promise internalsModule 3, ex. 5
withTimeout(p, ms)Promise.raceModule 3, ยง3.5
mapLimit(items, n, fn)bounded concurrencyModule 4, ex. 5
๐Ÿ’ก Where to drill more

BFE.dev (JavaScript-specific interview problems โ€” implement Promise.all, debounce, etc. with test suites), GreatFrontEnd, LeetCode's JavaScript 30-day challenge, and Lydia Hallie's JavaScript Questions repo (155 output-prediction puzzles with explanations).