DEV Community

David McCallum
David McCallum

Posted on

JavaScript Closures: A Comprehensive Guide

In this article, we will unravel the nuances of closures, illustrating their utility and functionality through a plethora of examples.

 

Prelude to Closures: Understanding Scope and Lexical Scope

 

Before we delve into closures, it's pivotal to have a firm grasp of scopes and lexical scopes in JavaScript.

 

Global Scope

 

In JavaScript, variables declared outside any function are in the global scope, accessible throughout the code.

var globalVariable = "I am a global variable";

function showGlobalVariable() {
  console.log(globalVariable); // "I am a global variable"
}

showGlobalVariable();
Enter fullscreen mode Exit fullscreen mode

 

Local Scope

 

Contrarily, variables declared within a function reside in a local scope, accessible only within that function and its nested functions.

function showLocalVariable() {
  var localVariable = "I am a local variable";
  console.log(localVariable); // "I am a local variable"
}

showLocalVariable();
// console.log(localVariable); // Error: localVariable is not defined
Enter fullscreen mode Exit fullscreen mode

 

Lexical Scope

 

Lexical scope, a static scope in JavaScript, means that a function's scope is physically determined by its location within the source code. Nested functions have the privilege to access variables declared in their outer scope.

function outerFunction() {
  var outerVariable = "I am an outer variable";

  function innerFunction() {
    var innerVariable = "I am an inner variable";
    console.log(outerVariable); // "I am an outer variable"
  }

  innerFunction();
  // console.log(innerVariable); // Error: innerVariable is not defined
}

outerFunction();
Enter fullscreen mode Exit fullscreen mode

 

Diving into Closures

 

When a function is created, a closure is formed alongside it. They're like two sides of the same coin but serve different roles.

  1. Function: The actual code that performs a specific task.
  2. Closure: The environment that "remembers" the variables the function needs to execute, even if the outer function has finished running.

So, while the function does the work, the closure makes sure it has access to all the tools (variables) it needs, no matter where it's called from. It's like a function carrying its own little environment with it.

 

Crafting Closures

 

Closures come into existence every time a function is created, at function creation time. To craft a closure, simply define a function inside another function and expose the inner function, either by returning it or passing it to another function.

 

Basic Closure Example

 

function createGreeting(greeting) {
  return function(name) {
    console.log(greeting + ", " + name);
  };
}

var sayHello = createGreeting("Hello");
sayHello("John"); // "Hello, John"
Enter fullscreen mode Exit fullscreen mode

In this instance, sayHello is a closure that encapsulates the greeting variable from its lexical environment.

 

Advanced Closure Example: Creating a Counter

 

Closures facilitate the creation of private variables and methods, safeguarding them from external access and preventing variable collisions.

function createCounter() {
  var count = 0;

  return {
    increment: function() {
      count += 1;
    },
    decrement: function() {
      count -= 1;
    },
    getCurrentCount: function() {
      return count;
    }
  };
}

var counter = createCounter();
counter.increment();
console.log(counter.getCurrentCount()); // 1
counter.decrement();
console.log(counter.getCurrentCount()); // 0
// console.log(counter.count); // undefined
Enter fullscreen mode Exit fullscreen mode

Here, count is a private variable, inaccessible directly from outside the createCounter function. The counter object exposes methods to interact with the count variable, forming a closure.

 

Practical Implementations of Closures

 

Closures find extensive applications in JavaScript, serving various purposes, including:

  1. Event Handling: Utilizing closures to remember the state in event handlers, thus avoiding global variable usage.
   document.getElementById('myButton').addEventListener('click', (function() {
     var clickCount = 0;
     return function() {
       clickCount += 1;
       console.log('Button clicked ' + clickCount + ' times');
     };
   })());
Enter fullscreen mode Exit fullscreen mode
  1. Module Pattern: Crafting modules with private methods and variables, promoting encapsulation and avoiding namespace pollution.
   var myModule = (function() {
     var privateVariable = "I am private";

     function privateMethod() {
       console.log(privateVariable);
     }

     return {
       publicMethod: function() {
         privateMethod();
       }
     };
   })();

   myModule.publicMethod(); // "I am private"
Enter fullscreen mode Exit fullscreen mode
  1. Currying: Transforming a function with multiple arguments into a series of functions each with a single argument, facilitating function reuse and composition.
   function multiply(a) {
     return function(b) {
       return a * b;
     };
   }

   var multiplyByTwo = multiply(2);
   console.log(multiplyByTwo(5)); // 10
Enter fullscreen mode Exit fullscreen mode

 

Conclusion

 

Closures, a cornerstone in JavaScript programming, offer a pathway to achieve privacy and state retention in your functions. A deep understanding of closures can elevate your coding prowess, enabling you to write more robust, maintainable, and efficient code. As you forge ahead in your JavaScript journey, endeavor to incorporate closures into your coding arsenal to unlock their full potential.

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

A closure in JavaScript is a function that has access to its own scope, the scope of the outer function, and the global scope.

If this definition were correct, there would be no point having a separate word for function and closure - since ALL functions have this access/behaviour.

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

Closures are created whenever any function is created, at the time of creation.

A closure and a function are two linked, but different things.

Collapse
 
mccallum91 profile image
David McCallum

@jonrandy Based on you explanation, I am understanding this:

When a function is created, a closure is formed alongside it. They're like two sides of the same coin but serve different roles.

  1. Function: The actual code that performs a specific task.
  2. Closure: The environment that "remembers" the variables the function needs to execute, even if the outer function has finished running.

So, while the function does the work, the closure makes sure it has access to all the tools (variables) it needs, no matter where it's called from. It's like a function carrying its own little environment with it.

Collapse
 
mccallum91 profile image
David McCallum

Thank you for providing the invaluable information.