Closures in JavaScript for Newbies

Muazzam Nashat
Jan 13 · 4 min read
Image for post
Image for post
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?

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?

“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.

The Startup

Medium's largest active publication, followed by +775K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store