Practical usage of JavaScript closure

There is a very common question about JavaScript closure that I understand the concept of JavaScript closure but what are the practical usages of this?

I completely get this part that I have access to the variables/functions defined in it’s parent scope but when do we need this?

Why we need closure in JavaScript

Closure in a Language is really powerful feature. In ECMAScript the functions are considered as first-class object. This means a function can be passed as a parameter to a function and a function can be returned from a function. When a function is passed to a function or returned from a function both the cases we get into scope issues (functional arguments issue). To solve this issue the closure is introduced in JavaScript.

Following are some good usage of JavaScript closure:

  • Accessing private variables and methods
  • Closure can be used to do module design pattern
  • Closures can be used in functional template
  • Closures can be used in functional JavaScript
Share