title: Javascript function scope [Javascript Essentials]
body: <p>I was watching Javascript Essentials by Travis Tidwell where he explained this piece of code:</p>
  
  <pre><code>(function() {
      var messages = ['hello', 'there'];
  
      for (var i in messages) {
          setTimeout(function() {
              console.log(messages[i]);
          }, 10); 
      };
  })();
  </code></pre>
  
  <p>It echoes 'there' twice in the console, but I still don't understand exactly why. Could somebody go through this piece of javascript with me step by step?</p>