JavaScript Closures Explained
Michael Mitrakos
4 min read
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach…
JavaScript Closures Explained
Having worked across sites raking in over 50 billion website visits annually with Higglo Digital I write about tech topics and teach engineers to have solid foundations that will help them get ahead in their career. I also build awesome products for digital nomads — check it out!
JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. Check out the ebook now!
Closures are a fundamental concept in Javascript that allows for the creation of functions that have access to variables defined outside of their scope. This allows for the creation of variables that are only accessible within a specific function or block of code, while still being able to access variables defined in the global scope.
To understand closures, it is important to first understand the concept of scope in Javascript. Scope refers to the accessibility of variables within a specific block of code. In Javascript, there are two types of scope: global scope and local scope. Global scope refers to variables that are defined outside of any function or block of code and are accessible anywhere in the program. Local scope refers to variables that are defined within a specific function or block of code and are only accessible within that specific function or block of code.
Now, let’s take a look at an example of a closure in action. In the following code, we define a function called createCounter that returns a new function called incrementCounter. The incrementCounter function is a closure because it has access to the “count” variable, which is defined outside of its scope in the “createCounter” function.
let count = 0;
}
}
counter(); // Outputs 2
counter(); // Outputs 3As you can see, the `incrementCounter` function is able to access and modify the `count` variable even though it is defined outside of its scope. This is possible because the `incrementCounter` function is a closure, meaning it has access to the variables defined in the `createCounter` function.
Closures are often used in Javascript for a variety of purposes, such as creating private variables within an object or maintaining state within a function. Here is another example of how closures can be used to create a private variable within an object:
return this.name;
}
}
In conclusion, closures are a powerful concept in Javascript that allows for the creation of functions with access to variables defined outside of their scope. This allows for the creation of private variables within objects, the maintenance of state within functions, and many other useful applications. Understanding how closures work and how to use them effectively is an essential skill for any Javascript developer.
#### JavaScript eBook
I’ve written an eBook on JavaScript that will take you from beginner to professional. Having been in your shoes moving to making over $200,000 per year in just a few years as a software engineer, I know exactly what it takes to get there. [Check out the ebook now](https://www.mitrakos.com/ebook)!
I founded [Higglo Digital](https://higglo.io) and we can help your business crush the web game with an award-winning website and cutting-edge digital strategy. If you want to see a *beautifully designed website*, [check us out](https://higglo.io).
I also created [Wanderlust Extension](http://www.wanderlustapp.io/) to discover the most beautiful places across the world with highly curated content. Check it out!