- Ausführende
-
Lernen
Python lernen Ausführbare Beispiele, NumPy, pandas und mehr KI / Machine Learning lernenNeuronale Netze, ML und LLMs von Grund auf C/C++ lernen Zeiger, Speicher, STL und modernes C++ C# lernenC#-Grundlagen, OOP, LINQ, Async und .NET-Übungen Matplotlib lernenLinien-, Balken-, Streu- und Kreisdiagramme mit interaktiven Visualisierungen Rust lernenOwnership, Traits, Fehlerbehandlung und Threads Go lernenSlices, Interfaces, Goroutinen und Channels Python-Algorithmen Suche, Sortierung, Rekursion und Visualisierungen O(n) C/C++-Algorithmen Implementierungen in C und C++ vergleichen Programmieren für Kinder Spielend programmieren lernen!
- QuizzeNeu
- AufgabenNeu
- Für LehrkräfteNeu
- Aktualisierungen
- Spendier mir einen Kaffee
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.
Beispielfrage
Frage 1 von 10What does this print?
const fns = [];
for (var i = 0; i < 3; i++) {
fns.push(() => i);
}
console.log(fns.map((f) => f()).join(","));
Lösung zeigen
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).
Darum geht es in diesem 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
Häufige Fragen
Brauche ich ein Konto?
Nein. Jeder kann spielen. Mit Anmeldung werden XP, Serie, Abzeichen und Verlauf gespeichert.
Kann ich es wiederholen?
Ja. Dein Bestwert bleibt erhalten, und Fragen und Antworten werden jedes Mal neu gemischt.