Manav Pandya
Explain closure in JavaScript
By Manav Pandya in JavaScript on Nov 26 2018
  • Rajendra Taradale
    Mar, 2019 4

    Function inside of another function which can access a value from it's parent function and do calculation without affecting any other operation like no matter how many time you call it's keep separate instance of it's own and parent, it's example of property inheritance.

    • 2
  • Kiran Mahajan
    Dec, 2018 3

    A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function’s variables?—?a scope chain.The closure has three scope chains:it has access to its own scope?—?variables defined between its curly brackets it has access to the outer function’s variables it has access to the global variables To the uninitiated, this definition might seem like just a whole lot of jargon!But what really is a closure? A Simple closure Let’s look at a simple closure example in JavaScript:function outer() {var b = 10;function inner() {var a = 20; console.log(a+b);}return inner; } Here we have two functions:an outer function outer which has a variable b, and returns the inner function an inner function inner which has its variable called a, and accesses an outer variable b, within its function body The scope of variable b is limited to the outer function, and the scope of variable a is limited to the inner function.Let us now invoke the outer() function, and store the result of the outer() function in a variable X. Let us then invoke the outer() function a second time and store it in variable Y.

    • 2
  • Bidyasagar Mishra
    Aug, 2019 2

    closures are the primary mechanism used to enable data privacy. When you use closures for data privacy, the enclosed variables are only in scope within the containing (outer) function. You can’t get at the data from an outside scope except through the object’s privileged methods. In JavaScript, any exposed method defined within the closure scope is privileged.

    • 1
  • kuldeep bhadoria
    Dec, 2018 16

    Well written Manav.Simply we can say Clousre basically inner function which is bounded by outer function and there is one variable we use which define upper of inner function,its initialised when we call first time and again we call the same function on that event then i will not initialize and only inner function will call and that variable worked for that element as global but does not exist after outer function.Scenario : - Like in JQuery if we want to increment one value of click of any button like plus image show values will increment but inthis case don't need to take global variable.Advantage :- dont need to create global variable.

    • 1


Most Popular Job Functions


MOST LIKED QUESTIONS