Question 1Easy
What is a closure in JavaScript?
- A function that runs immediately when defined and then discards its variables
- An object whose properties are hidden from code outside of its module
- A callback that the event loop runs after the current task has completed
- 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
- A closure happens when an inner function is defined inside an outer function.
- That inner function keeps a live reference to the outer function's variables, not just a snapshot of their values at creation time.
- 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 5Medium
What is the output?
let x = 'global';
function f() {
console.log(x);
let x = 'local';
}
try { f(); } catch (e) { console.log(e.name); }
- global
- undefined
- ReferenceError
- 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
- Inner let x shadows the global for the entire function.
- Access before the declaration is in the TDZ.
- 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()));
- [0, 1, 2]
- [2, 2, 2]
- [3, 3, 3]
- [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
- var i is a single variable.
- Loop ends with i = 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, 2, 3
- 0, 1, 2
- 0, 0, 0
- 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
- let, unlike var, creates a fresh, independent binding of the loop variable for EACH iteration of a for loop.
- So each setTimeout callback closes over its own separate copy of i, holding the value i had during that specific iteration.
- 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);
- number
- ReferenceError
- undefined
- 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
- let declares a variable that is scoped to the nearest enclosing block ({ ... }), not to the whole function or file.
- The variable x, declared inside the { } block, ceases to exist once that block ends.
- 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());
- 3, 3
- 2, 1
- 1, 2
- 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
- a and b have separate n variables.
- 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.