title: Difference between let and var inside setTimeout?
body: <p>I know the difference between let and var. let is block scope and var is functional scope.</p>
  
  <pre><code>for(var i=0; i&lt; 3; i++){
      setTimeout(function(){
          console.log(i);
      }, 10);
  }
  
  output : 3
           3
           3
  </code></pre>
  
  <p>I know how above code snippet is working(<code>console.log(i)</code> is executing at that time when value of i is 3, because scope of i is global).</p>
  
  <p><strong>But</strong></p>
  
  <pre><code>for(let i=0; i&lt; 3; i++){
      setTimeout(function(){
          console.log(i);
      }, 10);
  }
  
  output : 1
           2
           3
  </code></pre>
  
  <p>the above code snippet confusing me. according to me it should throw Reference Error(because the time when <code>console.log(i)</code> execute, will look the value of i in global scope not in local scope, and i is not declare/defined in global. so it should give reference error.)</p>
  
  <p>Anyone who can explain how 2nd for loop working on Runtime ?</p>