Skip to main content
JavaScript Interview Questions Predict Output P0 Medium

Loop callbacks and captured bindings

Predict both lines and the number of bindings each loop creates for the callbacks.

Official Reference

Code Snippet

const first = [], second = [];
for (var i = 1; i <= 3; i++) first.push(() => i * 2);
for (let j = 1; j <= 3; j++) second.push(() => j * 2);
console.log(first.map(fn => fn()).join(" "));
console.log(second.map(fn => fn()).join(" "));

Expected Output & Explanation

8 8 8
2 4 6

The first group shares i, which is 4 after its loop. The let loop gives each callback a different per-iteration binding.

Module Context & References

JavaScript · Language output drills Trace closures, receivers, prototypes, coercion and array edge cases
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.