JavaScript Quiz: Closures

This JavaScript closures quiz is built from interview questions. You will trace counters and function factories, solve the classic loop and setTimeout puzzle and see how closures keep data private.

कठिन 10 सवाल ~8 मिनट पास होने के लिए 80% Node.js 22

मोड चुनें

आप गेस्ट के तौर पर खेल रहे हैं: XP और streak सेव रखने के लिए साइन इन करें।

सैंपल सवाल

सवाल 1 / 10

What does this print?

function makeCounter() {
  let count = 0;
  return () => ++count;
}
const next = makeCounter();
next();
next();
console.log(next());
जवाब दिखाएं

D. 3 The returned function keeps a link to count even after makeCounter has finished. Each call increases the same count, so the third call returns 3.

इस क्विज़ में क्या है

  • A function remembers the variables of the scope where it was created
  • Each call to a factory function makes a new, separate closure
  • The var in a loop with setTimeout puzzle and its fixes
  • Closures capture variables, not values
  • Private state, currying and once()

अक्सर पूछे जाने वाले सवाल

क्या अकाउंट ज़रूरी है?

नहीं। कोई भी खेल सकता है। XP, streak, badge और हिस्ट्री सेव रखने के लिए साइन इन करें।

क्या मैं इसे दोबारा दे सकता हूं?

हां। आपका बेस्ट स्कोर सेव रहता है, और हर बार सवाल और जवाब शफ़ल होते हैं।