🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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());
  1. A.3 3
  2. B.1 1
  3. C.3 1
  4. 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 →