🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsKB5129194 Windows 11 26H1 Out of Band Update - Deskmodder.de(14.09.2026 um 19:25 Uhr)
🪟 Windows TippsGoogle Gemini: Neue Windows-App holt die KI aus dem Browser(14.09.2026 um 06:00 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsKB5129194 Windows 11 26H1 Out of Band Update - Deskmodder.de(14.09.2026 um 19:25 Uhr)
🪟 Windows TippsGoogle Gemini: Neue Windows-App holt die KI aus dem Browser(14.09.2026 um 06:00 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 4 Min Lesezeit
0

Understanding Closures in JavaScript: From Confusion to Clarity

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




Imagine This...



You’re working on a project, and you need a function to "remember" the values of variables—even after the function has finished running. Closures are like a magical backpack that lets you carry knowledge from a previous lesson wherever you go in your code. It’s one of the most powerful yet misunderstood concepts in JavaScript. But don’t worry—by the end of this guide, closures will go from head-scratching to “aha!”









What’s a Closure? Simplified Definition and Analogy



A closure is when a function "remembers" its surrounding state (the variables in its scope) even after the outer function has finished executing. Let’s break this down with a relatable analogy:






Real-Life Analogy: The Backpack of Knowledge



Imagine you’re a student. You have a backpack with your notes, pens, and books. You leave the classroom (your outer function), but you still have access to everything in your backpack (your closure). Whenever you need to solve a problem later, you can pull out the knowledge you saved in your backpack.






Simplified Definition



In JavaScript, closures happen when:




  1. A function is defined inside another function.

  2. The inner function "remembers" the variables of the outer function.









How Do Closures Work? Examples in Action



Let’s see closures in action with code examples.






Example 1: A Basic Closure






CODE
function outerFunction() {
const outerVariable = 'Hello, Closure!';

function innerFunction() {
console.log(outerVariable);
}

return innerFunction;
}

const myClosure = outerFunction();
myClosure(); // Output: "Hello, Closure!"






What’s Happening Here?





  • innerFunction is returned from outerFunction.

  • Even though outerFunction has finished executing, innerFunction still remembers outerVariable.









Example 2: Closure for a Counter






CODE
function createCounter() {
let count = 0;

return function () {
count++;
return count;
};
}

const counter = createCounter();
console.log(counter()); // Output: 1
console.log(counter()); // Output: 2






What’s Happening Here?




  • The returned function "remembers" the count variable, even though createCounter has finished running.

  • Each time you call counter, it updates and remembers the latest value of count.









Example 3: Closures in Loops (Common Pitfall)



Closures often trip up developers when used inside loops.




CODE
for (let i = 1; i <= 3; i++) {
setTimeout(() => console.log(i), i * 1000);
}
// Output: 1, 2, 3 (each after 1 second)






Why Does This Work?




  • The let keyword creates a block scope, so each iteration has its own i.

  • If you used var instead of let, all outputs would be 4 because var doesn’t create block scope.




📄 Documentation:


  • Connect on LinkedIn

  • Vollständiger Original-Bericht
    Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
    ↗ Original-Artikel auf dev.to lesen
    Wie bewertest du diesen Beitrag?
    1 Klick Feedback
    Teilen mit Netzwerk & Team:

    Community-Analysen & Experten-Meinungen 0

    Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
    Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
    Community Pulse: Relevanz-Einschätzung
    1 Klick Experten-Votum
    🔴 Akute Relevanz 0%
    🟡 In Evaluierung 0%
    🟢 Keine Auswirkung 0%
    Spannende Innovation 0%
    Verwandte Story-Cluster & Quellen (Vektor-KI)
    Port 8095 Engine
    1 Quelle
    The Gemini desktop app is now available for Windows
    1 Quelle
    Burn Out, Or Fade Away
    1 Quelle
    Windows 11 KB5129195 is out after Microsoft confirms major issues with the September 2026 update, but it won’t fix AMD GPU errors
    Ähnliche Beiträge
    🔍 Verwandte News

    Auch interessante Nachrichten Understanding Closures in JavaScript: From Confusion to Clarity

    Thematisch verwandte Begriffe: Understanding, Closures, JavaScript, From · 6 Treffer

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...