Skip to main content

Closures and lexical scope

Define a closure using a counter factory, then explain why separate counters do not share state.

Official Reference

Model Answer & Explanation

A function retains access to bindings in the lexical environment where it was created. Each factory call creates a separate environment, so two returned counters can retain separate state. A closure captures access to bindings, not a frozen copy of every value.

Follow-up Interview Question · Why can an event listener keep otherwise unused data alive?

Module Context & References

JavaScript · Language theory Explain scope, closures, this, prototypes, equality and references
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.