title: Difference in output using for loop & while loop using settimeout function javascript
body: <p>I'm aware of the difference between the var &amp; let variable declaration. I just executed the same piece of code using for loop &amp; while loop. To my surprisingly I got the different output &amp; I'm not able to differentiate the execution behavior difference between them.</p>
  
  <p>I have attached 2 code snippets using While loop &amp; For loop.</p>
  
  <p>If anyone is aware of this then please help me.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let i = 0;
  while(i &lt; 5) {
    setTimeout(function() {
      console.log(i)
    }, 1000);
    i++;
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>for(let j = 0; j &lt; 5; j++) {
    setTimeout(function() {
      console.log(j)
    }, 1000);
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Thanks!</p>