- JavaScript
- Execution Contexts, Closures, and Object Prototypes
JavaScript Closures & this Interview Questions
JavaScript closures interview questions are where the difference between a mid-level and senior frontend engineer shows, along with execution contexts, dynamic `this` resolution and prototype chain delegation. Rather than relying on memorized rules, candidates must reason about how the JavaScript engine allocates Lexical Environments during function creation and invocation.
Here, you will visualize the mechanics of closures retaining outer variables, step through the 4 rules of `this` binding at call-sites, and trace prototype lookups directly to null. These concepts are directly applicable to building resilient, bug-free components.
JavaScript Closures & this Interview Questions: 6 lessons
1. Execution Context & Call Stack
Interactive ReadyA JavaScript Execution Context is an internal runtime environment that tracks code evaluation, consisting of a Lexical Environment, Variable Environment, and ThisBinding. The Call Stack coordinates execution contexts using LIFO discipline: invoking a function pushes a context, and returning pops it off.
2. Closures & Lexical Scope
Interactive ReadyClosures in JavaScript bundle a function with references to its surrounding lexical environment (its [[Environment]] internal slot). Closures allow an inner function to access outer scope variables even after the outer function has finished executing and its stack frame has been popped.
3. this, call, apply & bind
Interactive ReadyThe this keyword in JavaScript represents the evaluation context of a function call, determined dynamically by the call-site: default binding (global/undefined), implicit binding (object before the dot), or explicit binding (via call, apply, or bind). Arrow functions have no own "this" and resolve it lexically.
4. Prototypes & Inheritance
Interactive ReadyEvery object has a prototype in JavaScript: inheritance works through prototype delegation, where each object holds an internal [[Prototype]] link to another object. Property access traverses this prototype chain upward to Object.prototype, while property assignment creates an own property on the receiver object, shadowing prototype properties.
5. Property Flags & Descriptors
Interactive ReadyJavaScript property descriptors govern property behavior through attribute flags: value, writable, enumerable, and configurable. Object.defineProperty allows fine-grained control over property mutability, loop visibility, and deletion behavior, defaulting omitted attributes to false.
6. IIFE: Immediately Invoked Functions
Interactive ReadyAn IIFE in JavaScript (Immediately Invoked Function Expression) is a function expression that runs as soon as it is defined: (function () { ... })(). Its variables stay private to that call, so it was the standard way to create private state and modules, and to capture loop values with var, before let, const and ES modules.
