- Esecutori
-
Impara
Impara Python Esempi eseguibili, NumPy, pandas e altro Impara l'IA / Machine LearningReti neurali, ML e LLM da zero Impara C e C++ Puntatori, memoria, STL e C++ moderno Impara C#Fondamenti, OOP, LINQ, async ed esercizi .NET Impara MatplotlibGrafici a linee, barre, dispersione e torta con visualizzatori Impara RustOwnership, trait, gestione degli errori e thread Impara GoSlice, interfacce, goroutine e canali Algoritmi Python Ricerca, ordinamento, ricorsione e visualizzazioni O(n) Algoritmi C e C++ Confronta le implementazioni in C e C++ Programmazione per bambini Impara a programmare giocando!
- QuizNuovo
- SfideNuovo
- Per docentiNuovo
- Aggiornamenti
- Offrimi un caffè
Quiz di JavaScript: 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.
Domanda di esempio
Domanda 1 di 10What does this print?
const fns = [];
for (var i = 0; i < 3; i++) {
fns.push(() => i);
}
console.log(fns.map((f) => f()).join(","));
Mostra la risposta
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).
Cosa copre questo quiz
- Why
varin a loop gives every closure the same variable - The per-iteration binding of
letinforloops - Fixing the loop with an IIFE or a callback parameter
- Variables declared outside the loop
constin a classicforloop
Domande frequenti
Serve un account?
No. Chiunque può giocare. Accedi per salvare XP, serie, badge e cronologia.
Posso rifarlo?
Sì. Il tuo miglior punteggio resta salvato, e domande e risposte vengono mescolate ogni volta.