JavaScript Quiz: Closures in Loops

This JavaScript quiz is all about the "closures in a loop" interview question. You will predict what functions created inside for and while loops remember, see why var and let behave differently, and pick the right fix.

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

मोड चुनें

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

सैंपल सवाल

सवाल 1 / 10

What does this print?

const fns = [];
for (var i = 0; i < 3; i++) {
  fns.push(() => i);
}
console.log(fns.map((f) => f()).join(","));
जवाब दिखाएं

C. 3,3,3 var is function scoped, so there is one i shared by all three closures. They run after the loop has finished, when i is 3 (the value that ended the loop).

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

  • Why var in a loop gives every closure the same variable
  • The per-iteration binding of let in for loops
  • Fixing the loop with an IIFE or a callback parameter
  • Variables declared outside the loop
  • const in a classic for loop

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

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

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

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

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