JavaScript closure interview questions.

Closures are the most asked JavaScript concept, because so much is built on them: private state, function factories, once, memoize, debounce and every React hook. These questions start with predicting what scope and loop snippets print, then have you write the utilities that depend on closures, then fix the stale closures that catch people out in React.

8 of these questions have free answers; the rest are in Pro. Updated .

58

questions

Easy
20
Medium
36
Hard
2

What interviewers look for

  • You can say what a closure captures: the variable itself, not a copy of its value.
  • You explain the var loop bug and fix it with let, a function argument or bind.
  • You use closures for private state and know the memory they keep alive.
  • You spot a stale closure in a hook and fix it with dependencies, a ref or a state updater.

Concept questions, answered

The definitions and comparisons interviewers open with. Free to read, each with a full answer and examples.

What is a closure in JavaScript?

A closure is a function together with the variables from the scope where it was created. In JavaScript every function keeps a live reference to its surrounding scope, so it can read and update those variables even after the outer function has returned. That is how private state, function factories, once, debounce and memoize work.

Read the full answer

What is lexical scope and the scope chain in JavaScript?

Lexical scope means the variables a piece of code can see are decided by where that code is written in the source, not by where it is called from. Scopes nest: global, then functions, then blocks. When you use a name, JavaScript looks in the current scope, then the one around it, and so on outward to the global scope. That ordered list of scopes is the scope chain.

Read the full answer

What is the difference between var, let and const?

var is function scoped, can be redeclared and is hoisted with the value undefined. let and const are block scoped, cannot be redeclared in the same scope and cannot be used before their declaration. let can be reassigned, while const cannot be rebound. Use const by default, let when you must reassign, and avoid var.

Read the full answer

What is hoisting in JavaScript?

Hoisting is how JavaScript registers declarations before it runs any code in a scope. The engine reads the whole scope first and creates the names, so you can use some of them above the line that declares them. Function declarations are fully usable early, var gives you undefined, and let, const and class throw an error.

Read the full answer

What is an IIFE in JavaScript?

An IIFE (immediately invoked function expression) is a function that is defined and called in the same expression, such as (function () { ... })(). It runs once, straight away, and gives the code inside its own scope. Before let, const and ES modules, that was the only way to keep variables private instead of leaking them into the global scope.

Read the full answer

What is currying in JavaScript?

Currying turns a function that takes several arguments into a chain of functions that each take one, so f(a, b, c) becomes f(a)(b)(c). Each call returns a new function until all arguments have arrived. It works through closures and makes it easy to build specialised functions from general ones.

Read the full answer

How does the this keyword work in JavaScript?

In JavaScript, this is decided by how a function is called, not where it is written. A method call obj.fn() sets this to obj, new Fn() sets it to the new object, call, apply and bind set it explicitly, and a plain call leaves it undefined in strict mode. Arrow functions ignore these rules and use the this of the code around them.

Read the full answer

Easy20

Medium36

Practise with tests and full answers.

Every question has a runnable editor, tests and a step-by-step solution. Passes from ₹149, paid once; nothing renews.

Get full access
Rehearse it in a timed JavaScript mock interviewA real editor with tests, the follow-ups an interviewer asks, and a score out of 100.

Frequently asked

What closure questions are asked in JavaScript interviews?

Define a closure and give an example, predict what a for loop with var and setTimeout prints and fix it, write a counter with private state, implement once, memoize or debounce, explain the module pattern, and in React, explain why a timer or event handler reads an old value of state.

Do closures cause memory leaks?

Only when something long-lived keeps a closure that holds on to large data: an event listener that is never removed, a timer that is never cleared, or a cache that never evicts. Remove the listener, clear the timer, or store less in the closure.

What is a stale closure in React?

A callback that was created during an earlier render and still reads that render's state and props. It shows up in effects with missing dependencies and in intervals. Fix it by adding the dependency, using the functional form of setState, or reading the latest value from a ref.