title: One all-encompassing definition of JavaScript closure
body: <p>I have read 10s of SO references on closures, MDN references and other blog articles. They all seem to define closures in their own ways. For example, From MDN documentation:</p>
  <pre><code>function makeFunc() {
    var name = &quot;Mozilla&quot;;
    function displayName() {
      alert(name);
    }
    return displayName;
  }
  
  var myFunc = makeFunc();
  myFunc();
  </code></pre>
  <hr />
  <p>And  here goes their explanation of closure:</p>
  <blockquote>
  <p>Normally, the local variables within a function only exist for the duration of that function's execution. Once makeFunc() has finished executing, it is reasonable to expect that the name variable will no longer be accessible. Since the code still works as expected, this is obviously not the case.</p>
  <p>The solution to this puzzle is that myFunc has become a closure. A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. The environment consists of any local variables that were in-scope at the time that the closure was created. In this case, myFunc is a closure that incorporates both the displayName function and the &quot;Mozilla&quot; string that existed when the closure was created.</p>
  </blockquote>
  <p>This below StackOverflow post answers closures as a stack of visible scopes.
  <a href="https://stackoverflow.com/questions/12543395/what-types-of-scope-exist-in-javascript?rq=1">What types of scope exist in Javascript?</a></p>
  <p>Where I am confused: is closure an object? Or is it just a &quot;anomalous scoping situation&quot; where inner nested function has access to a variable defined outside of itself but local to the container parent function, even after parent function has already executed? Is closure an object referring to this nested function (scope) situation like <code>myFunc</code> or is it the inner function itself?</p>