InterviewStack.io LogoInterviewStack.io

JavaScript Scope, Closures and Context Questions

Comprehensive mastery of JavaScript scoping rules, closure mechanics, and execution context including the this binding. Candidates should understand lexical scope and the scope chain, differences between function scope and block scope, and the behaviors and hoisting implications of var, let, and const. Know how closures capture and retain variables from outer scopes, how closures enable function factories and data privacy, and common closure patterns such as module patterns, factory functions, immediately invoked function expressions, currying, and decorators. Be able to apply closures safely in asynchronous code including timers, callbacks, and promises, and reason about pitfalls such as closures in loops, accidental retention of large objects, memory and garbage collection consequences, hoisting related bugs, and performance tradeoffs. Understand execution context and the this binding, including how call site determines this in global calls, method calls, constructor calls, and event handlers, how arrow functions use lexical this, and how to use call, apply, and bind to control context. Interview tasks typically include reading and explaining existing closures, identifying and fixing closure related bugs, implementing small patterns or utilities that rely on closures and context, and discussing debugging and optimization strategies. This also covers framework relevant patterns such as custom hooks in component based frameworks where closures and context interact with lifecycle and state.

HardTechnical
28 practiced
Implement a simple EventEmitter class in JavaScript with on(event, listener, ctx), once(event, listener, ctx), off(event, listener, ctx) and emit(event, ...args). Requirements:1) Call listeners with ctx as this when provided, otherwise with the emitter as this.2) Allow removing listeners added with a ctx.3) Ensure once listeners are removed after first successful emit.Provide code and explain how closures store listener metadata and enable off to remove exactly the right handler.
EasyTechnical
28 practiced
What is a closure in JavaScript and when is it created? Provide a minimal JavaScript example where an inner function preserves private state across multiple invocations. Explain why the outer variable remains alive even after the outer function has returned.
MediumTechnical
28 practiced
Explain why arrow functions cannot be used as constructors in JavaScript and describe what happens to their prototype property. Provide a short code sample that attempts new on an arrow function and explain the resulting error. Then show the correct way to create a simple constructor-like function or class.
HardTechnical
22 practiced
Explain what happens when Function.prototype.bind is used on a constructor and then the bound function is invoked with new. Provide a concrete example demonstrating which prototype is used for the newly constructed object, and explain the ECMAScript behavior. Finally, show a practical workaround to create a bound-like constructor that preserves prototype behavior for instances.
MediumTechnical
26 practiced
You observe steadily increasing memory usage in a single-page app. One suspect is a module that registers event listeners and stores large DOM nodes or data structures inside a closure that lives for the app lifetime. Describe how you would inspect and confirm this leak using browser devtools, show an example of problematic code, and explain two concrete fixes that would allow GC to reclaim the memory.

Unlock Full Question Bank

Get access to hundreds of JavaScript Scope, Closures and Context interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.