DEV Community

Cover image for Mystery of Closures in JavaScript!
Sudhil Raj K
Sudhil Raj K

Posted on • Edited on

Mystery of Closures in JavaScript!

At first glance, closures might seem like a complicated concept, but don’t worry they’re much simpler than they appear! Let’s strip away the confusion and address some common myths about closures.

Common Myths About Closures

Closures are special functions with access to their surrounding scope.
👎🏻Not true

  • A closure is not the same as a function.
  • In JavaScript, every function inherently has access to the scope in which it was defined. This is just how functions work!

Closures require nested functions.
👎🏻Wrong again

  • Nesting functions has nothing to do with creating closures.
  • Whenever a function is created, it automatically forms a closure with its surrounding scope, no nesting required.

What’s the Reality❓

Closures are not some magical or unique feature. Instead, they’re just the combination of a function and the variables from the scope in which the function was created. Every function comes bundled with this context—think of it as a memory of where it came from.

Here’s the MDN-approved definition:

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).

In plain English, a closure lets your function "remember" the variables from the place where it was created, even if it’s executed elsewhere.

Let’s See Closures in Action 🧑🏻‍💻

Here’s a fun little example:

function createCounter() {
    let count = 0; // This is the surrounding state

    return function() { // The inner function
        count++; 
        return count;
    };
}

const counter = createCounter(); // Create a counter instance
console.log(counter()); // Output: 1
console.log(counter()); // Output: 2
console.log(counter()); // Output: 3
Enter fullscreen mode Exit fullscreen mode

What’s Happening Here? 🤔
When createCounter is called, a variable count is created inside its scope.
The returned function remembers count, even though createCounter has already finished running.
Each time counter is called, it can access and update count because of the closure.

Why Do Closures Matter? 🔥

Closures aren't just a theoretical concept for acing interviews—they’re incredibly useful! Here are a few real-world scenarios:

1. Data Privacy

Closures can keep variables private and inaccessible from the outside world.

function secretMessage() {
    let message = "This is a secret!";
    return function() {
        return message; // Only this function can access the variable
    };
}

const getMessage = secretMessage();
console.log(getMessage()); // Output: "This is a secret!"
console.log(message); // Error: message is not defined
Enter fullscreen mode Exit fullscreen mode

2. Event Handlers

Closures are handy for event listeners when you want to "remember" some state.

function greetUser(username) {
    return function() {
        console.log(`Hello, ${username}!`);
    };
}

const greetJohn = greetUser("John");
document.getElementById("myButton").addEventListener("click", greetJohn);
// Even after greetUser is done, greetJohn remembers "John"
Enter fullscreen mode Exit fullscreen mode

3. Partial Application

Closures let you pre-set arguments for a function.

function multiply(a) {
    return function(b) {
        return a * b; // "a" is remembered
    };
}

const double = multiply(2);
console.log(double(5)); // Output: 10
console.log(double(10)); // Output: 20
Enter fullscreen mode Exit fullscreen mode

Wait, Are Closures Perfect❓

Not always! If you're not careful, closures can lead to memory leaks by keeping references to variables longer than necessary. For example, when closures are used inside loops, it’s easy to create unintended behaviors:

for (var i = 0; i < 3; i++) {
    setTimeout(function() {
        console.log(i); // Output: 3, 3, 3
    }, 1000);
}
Enter fullscreen mode Exit fullscreen mode

To fix this, you can use let (block scope) or an IIFE:

for (let i = 0; i < 3; i++) {
    setTimeout(function() {
        console.log(i); // Output: 0, 1, 2
    }, 1000);
}
Enter fullscreen mode Exit fullscreen mode

Wrapping It Up 🤲🏻💡

Closures are everywhere in JavaScript. Whether you realize it or not, you’re already using them! They’re the secret sauce that makes JavaScript powerful and flexible.

Here’s What to Remember: 🧠

  • Closures are not functions; they’re a function + the variables it remembers.
  • Every function in JavaScript has a closure.
  • Use closures for encapsulation, event handling, partial application, and more.

Thanks for reading. 🙌
Happy Coding! 🚀👨‍💻✨

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️
Collapse
 
sudhil profile image
Sudhil Raj K

@jonrandy Hello Brother, Actually lifted the content a bit.😉 I am planning to do a series on these type of core concepts. This one is the first!🙏🏻