title: Return a value from a Javascript closure function?
body: <p>I'm having trouble returning a value from a closure function in Javascript. Originally I had a return statement in the inside function, but that didn't seem to work. I moved the return statement to the outer function, and it still returns undefined. Shouldn't returnVal be given a value in the inner function before being returned in the outer function?</p>
  
  <pre><code>console.log('Returned: ' + getColumn('CPC'));
  
  function getColumn(header) {
    var returnVal = undefined;
  
    ptor.findElements(protractor.By.className('ngHeaderText')).then(function(headers) {
  
      for (var i = 0; i &lt; headers.length; i++) {
        (function(i){
          headers[i].getText().then(function(headerResult) {
            if (headerResult == header) {
              console.log('i: ' + i);
              returnVal = i;
            }
          });
        })(i);
      }
    });
  
    return returnVal;
  };
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>EDIT: moved the return statement</p>