Coding in JavaScript without an understanding of closures is like trying to speak English without an understanding of grammar rules — you might be able to get your ideas across, but probably a bit awkwardly.
Closure is one of important concepts in JavaScript. It is widely discussed and still confused concept. Let's understand what the closure is.
What are Closures?
As stated in MDN Web Docs, 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.
What is Lexical Scope?
Lexical scope is the ability for a function scope to access variables from the parent scope. We call the child function to be lexically bound by that of the parent function.
A Simple closure
Let’s look at a simple closure example in JavaScript:
function OuterFunction() {
var outerVariable = 1;
function InnerFunction() {
alert(outerVariable);
}
InnerFunction();
}
In the above example, InnerFunction() can access outerVariable.
Now, InnerFunction() can access outerVariable even if it will be executed separately. Consider the following example.
function OuterFunction() {
var outerVariable = 100;
function InnerFunction() {
alert(outerVariable);
}
return InnerFunction;
}
var innerFunc = OuterFunction();
innerFunc(); // 100
In the above example, return InnerFunction; returns InnerFunction from OuterFunction when you call OuterFunction(). A variable innerFunc reference the InnerFunction() only, not the OuterFunction(). So now, when you call innerFunc(), it can still access outerVariable which is declared in OuterFunction(). This is called Closure.
Above example also shows that the local variables are not copied in the closure: the closure maintains a reference to the original variables themselves. It is as though the stack-frame stays alive in memory even after the outer function exits.
Closure Scope Chain
Every closure has three scopes:
- Local Scope (Own scope)
- Outer Functions Scope
- Global Scope
When to use Closure?
Closures are useful whenever you need a private state associated with a function. This is a very common scenario - and remember: JavaScript did not have a class syntax until 2015, and it still does not have a private field syntax. Closures meet this need.
Performance considerations
It is unwise to unnecessarily create functions within other functions if closures are not needed for a particular task, as it will negatively affect script performance both in terms of processing speed and memory consumption.
Final points:
- Whenever a function is declared in JavaScript closure is created.
- Returning a function from inside another function is the classic example of closure, because the state inside the outer function is implicitly available to the returned inner function, even after the outer function has completed execution.
- A closure in JavaScript is like keeping a reference (NOT a copy) to the scope at the point of function declaration, which in turn keeps a reference to its outer scope, and so on, all the way to the global object at the top of the scope chain.
- A new set of local variables is created every time a function is called.
I hope this helps someone out there.
If you liked this post, you can find more by:
- Following me on Twitter: @forkbikash
- Following me on GitHub: @forkbikash
- Following me on this beautiful platform: @forkbikash
Top comments (2)
Thank you for your post
Closures in callback are perfect use case :
You can test it here : Counter
Yeah! absolutely.
Thanks for your appreciation @artydev .