title: IIFEs as closures
body: <p>In the <a href="https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch5.md" rel="nofollow noreferrer">You Don't Know Javascript</a> series, 1/3 of the way down IIFE's are described as not being closures themselves, but only if they're executed outside their lexical scope:</p>
  
  <blockquote>
    <p>Chapter 3 introduced the IIFE pattern. While it is often said that
    IIFE (alone) is an example of observed closure, I would somewhat
    disagree, by our definition above.</p>
    
    <p>This code "works", but it's not strictly an observation of closure.
    Why? Because the function (which we named "IIFE" here) is not executed
    outside its lexical scope. It's still invoked right there in the same
    scope as it was declared (the enclosing/global scope that also holds
    a). a is found via normal lexical scope look-up, not really via
    closure.</p>
  </blockquote>
  
  <pre><code>var a = 2;
  
  (function IIFE(){ // not actually a "closure"
      console.log( a ); 
  })();
  </code></pre>
  
  <p>In this <a href="https://stackoverflow.com/questions/2728278/what-is-a-practical-use-for-a-closure-in-javascript">SO post</a>, the following snippet was given as an example of a closure: </p>
  
  <pre><code>for (var i = 0; i &lt; someVar.length; i++)
      (function (i) {
          window.setTimeout(function () { 
              alert("Value of i was "+i+" when this timer was set" )
          }, 10000);
      })(i); 
  </code></pre>
  
  <p>I am trying to understand this in terms of the definition of closure (as defined in <a href="https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-closure-b2f0d2152b36" rel="nofollow noreferrer">this medium article</a>):</p>
  
  <blockquote>
    <p>To use a closure, simply define a function inside another function and
    expose it. To expose a function, return it or pass it to another
    function.   ... </p>
    
    <p>The inner function will have access to the variables in the outer
    function scope, even after the outer function has returned.</p>
  </blockquote>
  
  <p>I understand that a closure is a "stateful function", and that it is </p>
  
  <blockquote>
    <p>a way to "remember" and continue to access a function's scope (its
    variables) even once the function has finished running.</p>
  </blockquote>
  
  <p>So in this example, I see that the loop's <code>i</code> is remembered when passed into the closing IIFE.</p>
  
  <p><strong>My question is:</strong> </p>
  
  <p>Where is the "passing to another function or returning" portion happening? My guess is that the IIFE is able to remember the outer <code>i</code> for loop value at each iteration because the IIFE is passed to the window?</p>
  
  <p>Basically, my understanding is that a closure is <em>defined</em> as remembering an outer scope's value after the Garbage collector cleans that outer scope up, and that the <em>usage</em> of it is to expose the closure by returning it and accessing it outside its lexical scope. Is this correct? So where is that "accessing it outside the lexical scope" happening?</p>