title: Storing a cumulative value to a variable from within a function that's in a for loop, then recalling that variable elsewhere
body: <p>I'm having trouble understanding a particular concept. </p>
  
  <p>The general problem is storing a cumulative value to a variable from within a function that's in a <em>for loop</em>, then recalling the variable elsewhere.</p>
  
  <p>I'll explain all of the parts first:</p>
  
  <p>I call to an API to get the value of a key. The call looks something like this.</p>
  
  <pre><code>keyName = API.key('keyName'); 
  keyName.get(function(err, value) {
     console.log(value);
   });
  </code></pre>
  
  <p>However, I need to get a whole bunch of these keys, so I put the call into a <em>for loop</em>. I also want to store the cumulative value in the variable <em>fullString</em></p>
  
  <pre><code>var fullString = [];
  
  for (var i = 0; i &lt; numberOfKeys; i++) {      
    keyName = API.key('keyName' + i);
    keyName.get(function(err, value) {
      fullString += value;
  
      console.log(fullString);
    });
  }
  </code></pre>
  
  <p>Let's say:</p>
  
  <pre><code>keyName0 : 'a'
  keyName1 : 'b'
  keyName2 : 'c'
  </code></pre>
  
  <p>When the loop runs, I'll get this (which I understand):</p>
  
  <pre><code>a
  ab
  abc
  </code></pre>
  
  <p>The thing that I don't understand is, if I reference <em>fullString</em> outside of the function it returns <em>null</em> i.e.</p>
  
  <pre><code>var fullString = [];
  
  for (var i = 0; i &lt; numberOfKeys; i++) {      
    keyName = API.key('keyName' + i);
    keyName.get(function(err, value) {
      fullString += value;
    });
    console.log(fullString);
  }
  console.log(fullString);
  </code></pre>
  
  <p>Both of the above console.log's will return <em>null</em>, shouldn't they log the full value i.e. <em>abc</em> </p>