← Quiz: Scope, Closures, Hoisting and this: all 15 questions
Quiz: Scope, Closures, Hoisting and this · Question 2 of 15
What does this print? (code)
What does this print?
function counter() {
let n = 0;
return () => ++n;
}
const a = counter();
const b = counter();
a(); a();
console.log(a(), b());
- A.3 3
- B.1 1
- C.3 1
- D.4 2
Answer & explanation
Correct answer: 3 1
Each call to counter creates its own n; a has been called three times, b once.
Can you get the rest right without peeking?
Take the timed Quiz: Scope, Closures, Hoisting and this quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →