Submitted by admin on Wed, 05/12/2021 - 04:46
Clouser In JS Image

What are closures in javascript

Closure is nothing but the inner function having the access to variables and parameters of its outer function. Even after outer function has returned.

To understand closures better you should be familiar with the fundamental terms scope and lexical scope. Once you are familiar with scope and lexical scope you are one step ahead from closures.


scope is limiting the accessibility of variable and not having availability everywhere in the code. One of the major advantage of scope is some sort of security to the code, Scopes can be layered in a hierarchy in which child scopes have access to parent scopes.


The lexical Scope

Lexical scope has a set of rules for defining a variable in a program, when the program is compiled. These rules cannot be changed while the program is executing.

Javascript has a mechanism for scoping called lexical scoping.lexical scoping means the accessibility of variables and also the position of the variable inside the nested scopes.


  • Closures permit us to store data in different scope and allow it only when needed.
  • Closures allows us public varibles that are available even after the function task is completed.


  • closures allows us to create function inside a function,which leads to decrease memory speed due to duplication.
  • Closures make it complex to run Garbage collections,Which also leads to memory leak.

When to use Closures?

As we learned closures have advantages and disadvantages, it is also important to understand when we should use them.

  • when we are using reusable states.
  • Creating private namespace.
  • When implementing the module design pattern.


A closure is bundling of function together with reference to its surrounding state(lexical environment).In other words closure give access to outer scope of a function from an inner function.