Javascript closure function

Creating a closure is nothing more than accessing a variable outside of a function's scope. Please note that a function inside a function isn't a closure. Closures are always use when need to access the variables outside the function scope.

Closures mechanism is used to enable the data privacy.

Why is closures important?

  1. Closures provide a way to associate data with a method that operates on that data.
  2. They enable private variables in a global world.
  3. Many patterns, including the fairly popular module pattern, rely on closures to work correctly.

Answer will be 21. So here inside google function we are able to access the variable x which is outside the scope of the function google().

 

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *