Techoceanhub logo

Closures in Javascript

Closures in JavaScript are a powerful concept that involve functions and their access to variables in their outer (enclosing) function’s scope. Even after the outer function has finished executing, the inner function can still access and manipulate these variables. This creates a “closed-over” environment, hence the term closure.

Here’s an example to illustrate how closures work:

function createGreeter(name) {
  // Inner function that holds the name variable
  function displayName() {
    console.log("Hi, " + name + "!");
  }

  // Return the inner function
  return displayName;
}

// Call the outer function and store the returned function in a variable
const greetJohn = createGreeter("John");

// Call the inner function (now stored in greetJohn) even though the outer function has finished executing
greetJohn(); // Output: "Hi, John!"

In this example:

  • The createGreeter function takes a name argument.
  • Inside createGreeter, an inner function displayName is defined. This function uses the name variable from the outer scope.
  • Importantly, createGreeter returns the displayName function.
  • When we call createGreeter("John"), the function executes, creates the displayName function with the captured value of "John", and then returns displayName.
  • We assign the returned function to the variable greetJohn.
  • Now, even though createGreeter has finished executing, the greetJohn variable holds a reference to the inner function displayName.
  • When we call greetJohn(), it executes the displayName function, which still has access to the captured value of "John" from the outer scope. This allows us to greet John even after the outer function has finished.

Closures are useful in various scenarios:

  • Creating private variables: By using closures, you can create functions that have access to private variables within their scope, even if the outer function is called multiple times.
  • Simulating data encapsulation: Closures can help simulate data encapsulation by restricting access to certain variables within a specific function’s scope.
  • Event handlers: Closures are often used in event handlers to preserve the state of a variable at the time the event handler is attached.

Overall, closures provide a way for functions to remember and access variables from their creation context, even after the outer function has executed. This can lead to more flexible and modular code.

Share:

Categories

Archives

Related Posts

Array Destructuring

Array Destructuring Array destructuring is a powerful syntax introduced in ES6 that allows you to unpack elements from an array

Read More »

Explore our Affiliated partner links

hostgator website plan

Hostgator

Best Website hosting plans & Best for 24/7 support.

hostinger webhosting plan

Hostinger

Reasonable website hosting packages for small business & 24/7 Email support

Elementor pro

Design Worpdress website quickly using Elementor Builder and AI

buy elementor website templates

Buy Elementor Website Template

Buy ready made Elementor Website Template from us & Get Free Elementor pro licence for 1 year .