Dive into JavaScript Closures.
Tags: javascript, typescript, webdev, react
Published: 2022-12-27 04:07:57

The closure is one of the most important concepts alongside a confusing keyword in the world of javascript.

**_The closure is created every time a function gets created at function creation time.
It gives access to an outer function from the inner function._**

Let's try to dive into the concept and understand what exactly are closures and where it comes into play.

The definition of MDN, says "A closure is the combination of a **function** and the **lexical environment** within which that **function was declared**."

_**Closure is when a function can remember and access its lexical scope even when it's invoked outside its lexical scope.**_

Here, we need to understand the "lexical scope".

Lexical scope states how a variable name is resolved within nested functions.

Let's outline a basic closure.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v46wx50iklq1k1pydh5n.png)

outerFunc() creates a local variable called outer and a function called innerFunc(). The innerFunc() the function is an inner function that is defined inside outerFunc() and is available only within the body of the outerFunc() function. Note that the innerFunc() function has no local variables of its own.

However, since inner functions have access to the variables of outer functions, innerFunc() can access the variable outer declared in the parent function, outerFunc().

This is an example of lexical scoping, which describes how a parser resolves variable names when functions are nested. The word lexical refers to the fact that lexical scoping uses the location where a variable is declared within the source code to determine where that variable is available.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pigpegymha2anrfps49r.png)

In some programming languages, the local variables within a function exist for just the duration of that function's execution. Once outerFunc() finishes executing, you might expect that the outer variable would no longer be accessible.

However, because the code still works as expected, this is obviously not the case in JavaScript.

The reason is that JavaScript creates a "Closure". In the above case, myFunc is a reference to the instance of the innerFunc that is created when the myFunc gets executed.
The instance of innerFunc maintains a reference to its lexical environment, within which the variable name "outer" exists. For this due reason, when the myFunc is invoked, the variable "outer" remains available for use.

Let's take a slightly more complex example.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3fnj1lytx6nn5hlz8inw.png)

Can you guess what's going on with this code?

When you are trying to invoke the add5() method, it has reference to the anonymous function which accepts an argument "y" but makeAdder() accepts an argument "x". So, how this lexical scope is getting created.

Let's understand this.

If you see the add5() and add10() method creates a closure.

If you try to console the add5 method, it will display as below.

```
function(y) {
  window.runnerWindow.proxyConsole.log(y)
  return x + y;
}
```
The anonymous function still holds the value of "x" in the outer scope created from closure.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3cv7d7obhpytrgype3f.png)

I hope by this point, you might have started making sense of how tricky the concept of "Closures" is but at the same time it is also interesting.

Let's talk about the most common mistakes, developers make while understanding the closures.

We will take a most asked interview questions on the concept of "Closures", i.e. the canonical for loop example.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3cnbkzxnqdy3ir7l5wbj.png)

At the first glance, if you are learning "Closures" for the first time, most of the answers you will get are like.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v7u4ryqmi8g0etaegg1z.png)

But, what actually you will get is like.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ukvsathbz8bkie8u1am.png)

Now, the point here to ponder is Why? Let's talk about a few possible solutions.

1. Using the "let" keyword.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ak969fzey1w9i26dmwum.png)

Using the let keyword, every closure binds the block-scoped variable, meaning that no additional closures are required.

2. Using the "IIFE".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ya2u35ioc78thxl1o6fm.png)

If you see, we are using an IIFE function that takes an argument "index" which holds the current value of "i" at every iteration.
Closely analyzing the IIFE block, the setTimeout() function holds the value of "index" even after it has been executed which was created using a "closure".

That is the magic of using a "Closure".

3. Returning a "function" just like a "closure".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmh4mojr3x79t4f1re43.png)

If you understand the above example, the solution here is self-explanatory, the setTimeout() function holds the current value at every iteration. As we are returning a function with IIFE too, it prints the desired output.

Let's take another example of Closure.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4szmhfinetjy8kg1lcq.png)

I guess, now you would be able to make what should be the output?

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a97gp6t573w7vbjriff0.png)

If you answer is above, then let's understand what's going on?
If your answer is 1,2,3,4. I guess, you are on the right track.

So, let's try to understand what's going in the code block.

Always remember this point.

**_Closures - It is created when functions are created, not when they are invoked._**

Let's see when we are invoking the getItems[0]() on line # 13, let's start executing the code block.

An empty array is initialized on line #2.

Then the for loop on the line #3, variable "i" is hoisted. In other words, the "i" variable is declared outside of the for loop, and the variable is then mutated during each iteration.

For each iteration, we're pushing an anonymous function to the array. You can push a value, an object or a function to an array. If we pushed i to the array, we would push the value of i at the time of iteration to the array.

As we are pushing an anonymous function to the array, the anonymous function will return the value of "i" at the time of invocation. At the time of the invocation, the loop would have already run.

By the time the loop completes, "i" will be equal to 5 because of the way that the increment expression in a for loop works.

The only left out question is how can each anonymous function in the getItems array can access the value of i at each iteration?

The definite answer is to bind the value of "i" at each iteration and that too at the creation time and not the invocation time ???

Think about what's happening when we execute the anonymous function.

1. We're invoking an anonymous function from the global scope.
2. addItems on line 1 is an outer function that returns a bunch of inner functions, because the items array that is returned contains a bunch of anonymous functions that are essentially an array of inner functions in the addItems function
3. Each of these inner functions in the items array is accessing the i variable which is in the addItems function scope
4. At the time that each inner function is invoked, the addItems function no longer exists, yet each of the inner functions maintains access to the i variable that is in the addItems scope

So, all the above conditions for a closure have been met.

Now, only part left is to uniquely bind the "i" variable at each iteration.

We can solve using "let" keyword and IIFE.

_**Using "let" keyword.**_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/542brqfpieead3s1ujc6.png)

_**Using "IIFE"**_

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aocm09g6yv2cvf9srd1r.png)

By wrapping each items.push in an IIFE, we are ensuring that the j variable is privately scoped to this function, and as a result, the j variable will have a unique binding for each iteration.

I hope the above examples will help you a lot in understanding the "Closures".

Two important characteristics of determining a Closure.

1. _An outer wrapping function is invoked, to create the enclosing scope._
2. _The return value of the wrapping function must include reference to at least one inner function that then has closure over the private inner scope of the wrapper._

Let's see some practical use of the "Closures".

1. _Encapsulation - Emulating private methods_
2. _Common web examples like "Adding buttons to a page to adjust the text size"._
3. _Module pattern and Revealing Module pattern_
4. _Closures can be used to make functions more memory efficient and performant - for example, ensure that a large array/object is only initialized once._
5. _Closures are the fundamental concept of the functional programming. HOF, currying is not possible without closures._
6. _Init functions - closures can be used to ensure that a function is only called once._

I hope this article might have cleared most of the misconceptions around "closure". If you really like the article, please follow me.
Happy coding. Keep learning. Keep exploring.