Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is a closure in JavaScript

Understanding the Basics

In the world of programming, we often encounter a variety of concepts that may initially seem complex and overwhelming. One such concept in JavaScript is a 'Closure'. Now, before you start to panic, let's break it down for you.

A closure in JavaScript is a function that has access to its own scope, the outer function's scope, and the global scope. Think of it like a backpack you carry around. Imagine you're going to school, and in your backpack, you have the things you need - books, pens, and personal items. In JavaScript, this backpack is your closure, and the items are your variables and functions.

What is a Closure?

In simple terms, a closure is a function bundled with its lexical environment. Lexical environment? Now, that's a fancy term. It simply means the environment where the function was declared, which influences how variables are looked up. If you're still finding it difficult to grasp, think of it like your neighborhood. Your neighborhood is your lexical environment, and you, functioning within it, are the closure.

In JavaScript, when you create a function inside another function, you create a closure. Here's an example of a closure:

function outerFunction() {
  var outerVariable = 'I am outside!';

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

  return innerFunction;
}

var myNewFunction = outerFunction();
myNewFunction(); // logs: 'I am outside!'

In this example, outerFunction is the outer function, and innerFunction is the closure. The important thing to note here is that the closure, innerFunction, has access to outerVariable, a variable in its lexical environment.

How Does a Closure Work?

Remember the backpack analogy? Just like how you carry your backpack wherever you go, a closure 'carries' its scope wherever it is executed, no matter where it is called from.

Let's modify the previous example to demonstrate this:

function outerFunction() {
  var outerVariable = 'I am outside!';

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

  return innerFunction;
}

var myNewFunction = outerFunction();
outerVariable = 'Can I change?';
myNewFunction(); // still logs: 'I am outside!'

Even though we tried to change outerVariable after creating the closure, it still logged the original value. This shows that the closure 'remembers' its original environment.

Why Do We Use Closures?

Closures have several uses in JavaScript:

Data Privacy: Closures can be used to emulate private methods, a concept from object-oriented programming. In JavaScript, all variables can be accessed and modified. With closures, we can prevent direct access to variables.

State: Closures allow us to save state. This comes in handy in programming paradigms like functional programming and event-driven programming.

Event Handling: In JavaScript, closures are often used in event handlers and callbacks.

Conclusion

In the realm of JavaScript, closures are like your personal backpack, carrying all the tools (scope) you need. They 'remember' their original environment, and provide a way for functions to have 'private' variables. Just like how a backpack can help you in your journey to school, understanding closures can help you in your journey to becoming a better JavaScript developer.

So, the next time you come across a closure while coding in JavaScript, don't panic. Simply imagine it as your trusty backpack, always there with the tools you need, regardless of where you're journeying in the vast world of programming.