DEV Community

Cover image for Do you know Closure in JavaScript ?
Mohammad Aman
Mohammad Aman

Posted on

Do you know Closure in JavaScript ?

First of All, if you are a newbie and don't know What is JavaScript ?

JavaScript is a programming language primarily used for creating interactive web pages. It breathes life into static HTML, adding animations, dynamic content, and user interaction. Think of it as the bridge between the user and the website, making the browsing experience engaging and responsive.
According to several surveys, JavaScript currently holds the title of the most popular programming language in the world, with a vast user base and constantly evolving ecosystem.

And if you are familiar with JavaScript then you should must know what is closure in JavaScript

  • What is a Closure ? A closure is the combination of a function and its associated lexical environment. The lexical environment encompasses the variables, functions, and objects that were in scope when the function was created. This "enclosed" environment persists even after the outer function has finished executing, allowing the inner function to access and modify these variables
function createCounter() {
  let count = 0; // Outer function variable

  return function() {
    count++; // Inner function can access and modify count
    return count;
  };
}

const counter = createCounter(); // Get a new counter function

console.log(counter()); // 1
console.log(counter()); // 2
console.log(counter()); // 3

Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. createCounter createCounter defines an outer function with a count variable.
  2. It returns an inner function (the closure) that has access to count.
  3. counter stores a reference to the returned inner function.
  4. Each time counter() is called, it increments and returns the shared count value.

Real-world applications:

  • Module patterns: Creating private variables and methods within modules to maintain encapsulation and avoid naming conflicts.

  • Event handlers: Preserving state information between events (e.g., a button click counter).

  • Callbacks: Passing data to functions that will be executed later (e.g., asynchronous operations).

  • Partial application: Creating new functions with pre-filled arguments (e.g., a function that always greets a specific person).

  • Timers: Scheduling functions to run after a delay (e.g., animation effects).

  • Custom iterators: Implementing custom iteration logic for data structures (e.g., generators).

Key takeaways:

  • Closures allow functions to "remember" and access variables from their enclosing scope, even after the outer function has finished.
  • They are a powerful tool for creating private state, managing data, and implementing modular code structures.
  • Understanding closures is essential for writing more sophisticated and reusable JavaScript applications.

Top comments (4)

Collapse
 
610470416 profile image
NotFound404

A closure is a programmable scope where functions only change variables with the scope.

Collapse
 
610470416 profile image
NotFound404

a closure is not a function. just because in javascript a function can be a closure. You are totally mistaking one thing for another.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.