Closures in JavaScript for Newbies

Muazzam Nashat
The Startup
Published in
4 min readJan 13, 2021
Photo by Pankaj Patel on Unsplash

If you are a beginner in JavaScript, it is really important to understand the concept of closures. It will save you hours of debugging.

Before we dive into the world of closures, we need to make sure that we are comfortable with the idea of scope in JavaScript.

What is the scope?

Imagine that you are chatting with two friends, A and B, on Facebook. Whatever messages you send to A, B does not have access to them. The same goes for A. But if you create a group then everyone can see or have access to all the messages. This is because these chat windows have their own audiences or scopes. The same principle of scope exists in JavaScript. Whether you will have access to a variable from a particular position in your code, depends on the scope. The scope is created by functions and code blocks.

There are three types of scope in JavaScript … well, unless you define variables with the keyword var. We will get into this in a second. Before that let’s see an example of these different scopes.

Here, every variable declared outside the function testScope has a global scope. And any variable declared inside the function testScope has a functional scope. But if any variable is declared inside curly braces then it has its own scope which is called block scope. But what is meant by these scopes and who has access to whom? Let’s see!

First, we’ll see who has access to the global scope. With JavaScript, the global scope is the complete JavaScript environment. In HTML, the global scope is the window object.

This will console log:

This means we can access the global variable from any scope. You can think of global scope as the group chat! Everyone has access to the messages!

In the case of functional scope, we’ll only have access to the functional scoped variables only if we are inside the function. Here is an example:

Even if we have nested scopes inside a function, we’ll still have access to the functional scoped variable.

At this point, we should have a clear idea of what global and functional scopes are! If not, take a pause and read the code snippets again!

Now, let’s talk about the block scope. ES2015 introduced two important new JavaScript keywords: let and const. If you don’t have any idea about versions in JavaScript, you can read this.

These two keywords introduced block-scoped variables in JavaScript. Anything written inside a block {} (curly braces) are block-scoped.

BUT variables declared with keyword var are not block-scoped. So,

If you are interested in learning more about the differences between var, let, and const you can read this article.

Now that we have a clear understanding of all the scopes, let’s see an example with nested scopes.

Here, innerMostFunction() has access to all the variables of its outer scopes. But innerFunction() can’t access its inner scope variables.

To sum up the idea of scoping, we can say:

  • Global scoped variables can be accessed from anywhere in the codebase.
  • Block scopes variables are only accessible within the block.
  • The variables of the outer scope are accessible inside the inner scope.

But how does JavaScript know when we console.log innerScope or functionalScope, where to find them? JavaScript uses a scoping mechanism called lexical scoping or static scoping. In lexical scoping, the availability of the variables is determined by the position of the variables within the nested function scopes.

What is closure?

Now that we have understood the concept of scope in JavaScript, we are ready to discuss Closures. According to MDN:

“A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.”

Let’s understand the definition with some examples. We know that the outer scope variable can be accessed from the inner scope when the functions are nested. So far, we have been invoking the inner functions inside the outer function. What if we invoke the inner function outside the outer function?

Here, we are returning the innerFunction and then saving its reference to the test variable. Then when we call test(), it still has access to the functionalScope variable. This is because functions in JavaScript form closures. A closure is the combination of a function and the lexical environment within which that function was declared. This environment contains any local variables that were available within the scope when the closure was created. In our example, test() is a reference to the instance of the function innerFuntion() that was created when testScope() was run. The innerFuntion() maintains its lexical environment and that’s why when test() is run, it still has access to functionalScopecvariable.

I hope I was able to explain closures in the simplest way possible. If you are a newbie in the JavaScript world, this stuff might feel overwhelming, but I can assure you once you understand the fundamentals of JavaScript, you will fall in love with this language!

If you have any questions feel free to reach out to me on LinkedIn.

--

--

Muazzam Nashat
The Startup

Full stack developer experienced in Ruby and JavaScript based programming.