Scope & Closures Questions with Answers

15 practice questions on Scope & Closures (JavaScript) with answers and step-by-step solutions. Try 10 samples below, then practise all 15 free in the AptiNest app.

Sample questions

Question 1Easy

What is a closure in JavaScript?

  1. A function that runs immediately when defined and then discards its variables
  2. An object whose properties are hidden from code outside of its module
  3. A callback that the event loop runs after the current task has completed
  4. A function bundled with references to its surrounding scope's variables

Answer: D. A function bundled with references to its surrounding scope's variables

Explanation

A closure lets an inner function keep accessing variables of its outer function even after the outer function has returned.

Step-by-step solution

  1. A closure happens when an inner function is defined inside an outer function.
  2. That inner function keeps a live reference to the outer function's variables, not just a snapshot of their values at creation time.
  3. This reference persists even after the outer function has finished running and would otherwise have its local variables cleaned up.

Why the other options are wrong

  • A. An immediately-invoked function that discards its variables is the opposite of a closure, which specifically keeps those variables alive for later use.
  • B. Hiding properties from outside code describes encapsulation/module patterns, which often use closures as a mechanism but aren't the definition of a closure itself.
  • C. A callback scheduled after the current task is a general async/event-loop concept, unrelated to the specific closure mechanism.

Question 2Easy

What is the output?

var x = 1;
function f() { var x = 2; }
f();
console.log(x);
  1. 2
  2. undefined
  3. ReferenceError
  4. 1

Answer: D. 1

Explanation

The var inside f creates a separate local variable, so the global x stays 1.

Step-by-step solution

  1. The global x is 1.
  2. f declares its own x.
  3. Global x is untouched.

Why the other options are wrong

  • A. The inner var is local to f.
  • B. The outer x is initialised to 1.
  • C. The outer x is in scope.

Question 3Easy

What is the output?

function makeCounter() {
  let c = 0;
  return () => ++c;
}
const a = makeCounter();
a();
a();
const b = makeCounter();
console.log(a(), b());
  1. 3 1
  2. 2 1
  3. 1 1
  4. 3 3

Answer: A. 3 1

Explanation

Each call to makeCounter creates a new c. a has been called twice already, so its third call returns 3. b has its own counter and returns 1.

Step-by-step solution

  1. makeCounter() creates a new local variable c (starting at 0) and returns an arrow function that increments and returns it.
  2. Each call to makeCounter() creates a brand-new, independent c, because a new function call means a new local scope.
  3. a is called twice before the final line (reaching c=2), then a() in the final line makes it 3; b is a fresh counter, so its first call returns 1.

Why the other options are wrong

  • B. 2 1 would mean a's counter had only been incremented once before the final call, but it was actually called twice already (a(); a();) before the logged call.
  • C. 1 1 would mean both counters started completely fresh at the logging line, but a's counter already has two prior increments.
  • D. 3 3 would mean b somehow shared a's counter state, but each call to makeCounter() creates its own independent closure over its own c.

Question 4Easy

What is the output?

function counter() {
  let c = 0;
  return () => ++c;
}
const a = counter();
a(); a();
console.log(a());
  1. 1
  2. 3
  3. 0
  4. undefined

Answer: B. 3

Explanation

The arrow function closes over c, so it keeps counting: 1, 2, 3.

Step-by-step solution

  1. counter() returns a function holding c.
  2. Three calls increment c to 3.
  3. The third call returns 3.

Why the other options are wrong

  • A. c persists between calls through the closure.
  • C. ++c increments before returning.
  • D. The arrow function returns the incremented value.

Question 5Medium

What is the output?

let x = 'global';
function f() {
  console.log(x);
  let x = 'local';
}
try { f(); } catch (e) { console.log(e.name); }
  1. global
  2. undefined
  3. ReferenceError
  4. SyntaxError

Answer: C. ReferenceError

Explanation

The inner let x exists but is uninitialised until its declaration (temporal dead zone), so reading it throws ReferenceError.

Step-by-step solution

  1. Inner let x shadows the global for the entire function.
  2. Access before the declaration is in the TDZ.
  3. ReferenceError is thrown.

Why the other options are wrong

  • A. The inner let shadows x for the whole function body.
  • B. let is not initialised to undefined before its declaration.
  • D. The code is syntactically valid.

Question 6Medium

What is the output?

var fns = [];
for (var i = 0; i < 3; i++) fns.push(() => i);
console.log(fns.map(f => f()));
  1. [0, 1, 2]
  2. [2, 2, 2]
  3. [3, 3, 3]
  4. [undefined, undefined, undefined]

Answer: C. [3, 3, 3]

Explanation

All closures share one var i, which is 3 after the loop finishes.

Step-by-step solution

  1. var i is a single variable.
  2. Loop ends with i = 3.
  3. Each arrow reads i later: 3.

Why the other options are wrong

  • A. var shares one binding across iterations.
  • B. The loop ends when i becomes 3.
  • D. i is still in scope.

Tip: Use let in the loop header for a fresh binding per iteration.

Question 7Medium

What is logged, in order?

for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0);
}
  1. 1, 2, 3
  2. 0, 1, 2
  3. 0, 0, 0
  4. 3, 3, 3

Answer: B. 0, 1, 2

Explanation

let creates a fresh binding of i for each loop iteration, so each callback closes over its own value.

Step-by-step solution

  1. let, unlike var, creates a fresh, independent binding of the loop variable for EACH iteration of a for loop.
  2. So each setTimeout callback closes over its own separate copy of i, holding the value i had during that specific iteration.
  3. The callbacks therefore log 0, 1, 2 - the exact value i held when each iteration scheduled them.

Why the other options are wrong

  • A. 1, 2, 3 would be the result if the loop somehow incremented i one extra time before each callback's own snapshot was taken, which is not how let's per-iteration binding works.
  • C. 0, 0, 0 would be the result if, like var, all callbacks shared one variable frozen at its starting value - but let gives each iteration its own fresh copy instead.
  • D. 3, 3, 3 is the 'var' behavior (all callbacks sharing the one final value), the opposite of what let actually does here.

Tip: When you need each loop iteration's callback to remember that iteration's own value, prefer let over var.

Question 8Medium

What is the output?

{
  let x = 1;
}
console.log(typeof x);
  1. number
  2. ReferenceError
  3. undefined
  4. object

Answer: C. undefined

Explanation

let is block-scoped, so x does not exist outside the braces. typeof on an undeclared identifier returns "undefined" instead of throwing.

Step-by-step solution

  1. let declares a variable that is scoped to the nearest enclosing block ({ ... }), not to the whole function or file.
  2. The variable x, declared inside the { } block, ceases to exist once that block ends.
  3. Checking typeof x outside the block doesn't throw an error for an undeclared identifier; it simply returns "undefined".

Why the other options are wrong

  • A. "number" would require x to still be accessible outside the block where it was declared, but let confines it strictly to that block.
  • B. A ReferenceError would occur if you tried to directly use x (like console.log(x)) outside the block; typeof is special in that it safely returns "undefined" for any undeclared identifier instead of throwing.
  • D. "object" doesn't match any value type here; x holds a number (1) only inside the block, and is entirely gone outside it.

Tip: Block scoping with let/const means a variable's lifetime is tied strictly to the nearest { }, unlike var's function-wide scope.

Question 9Hard

What is the output?

function make() {
  let n = 0;
  return { inc: () => ++n, get: () => n };
}
const a = make(), b = make();
a.inc(); a.inc(); b.inc();
console.log(a.get(), b.get());
  1. 3, 3
  2. 2, 1
  3. 1, 2
  4. 0, 0

Answer: B. 2, 1

Explanation

Every call to make() creates a separate closure scope, so a has n = 2 and b has n = 1.

Step-by-step solution

  1. a and b have separate n variables.
  2. a.inc twice gives 2; b.inc once gives 1.

Why the other options are wrong

  • A. Each call to make creates its own n.
  • C. a was incremented twice, b once.
  • D. inc updates n.

Question 10Hard

What is the output?

let x = 1;
function outer() {
  let x = 2;
  return function () { return x; };
}
const f = outer();
x = 3;
console.log(f());
  1. undefined
  2. 3
  3. 2
  4. 1

Answer: C. 2

Explanation

JavaScript uses lexical scope: the inner function sees the x declared in outer, which is 2. Changing the global x does not affect it.

Step-by-step solution

  1. The inner function returned by outer() is defined inside outer's scope, so it forms a closure specifically over outer's local x (which is 2), not the global x.
  2. JavaScript resolves variable names lexically - based on where the code is WRITTEN, not where it is later called from.
  3. So even after the global x is reassigned to 3, calling f() still returns outer's own x, which remains 2.

Why the other options are wrong

  • A. undefined would require the inner function to have lost access to its closed-over x entirely, which doesn't happen; closures keep their reference alive.
  • B. 3 would require the inner function to be reading the GLOBAL x instead of outer's local x, but lexical scoping means it always refers to the x declared in its own enclosing scope (outer).
  • D. 1 would be the value of global x before it was reassigned, but the inner function never refers to global x at all - it has its own local x from outer.

Practise all 15 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all JavaScript topics · All topics