title: How Closure is getting applied in this code?
body: <p>In this code, nextVal seem to be remembered by the function but i am unable to understand.</p>
  
  <pre><code>var gimmeSomething = (function(){
          var nextVal;
  
          return function(){
              if (nextVal === undefined) {
                  nextVal = 1;
              }
              else {
                  nextVal = (3 * nextVal) + 6;
              }
  
              return nextVal;
          };
      })();
  
      gimmeSomething();       // 1
      gimmeSomething();       // 9
      gimmeSomething();       // 33
      gimmeSomething();       // 105
  </code></pre>