title: Why is the code returning undefined if I use var instead of let in for loop
body: <p>Why does my code return undefined if I use var inside a for loop instead of let. I have attached the output for let and var below the code blocks.</p>
  <pre><code>const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  
  for (let i = 0; i &lt; 10; i++) {
    setTimeout(() =&gt; console.log(a[i]), 3000);
  }
  </code></pre>
  <p>Output : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</p>
  <pre><code>var i = 0;
  for (i = 0; i &lt; 10; i++) {
    setTimeout(() =&gt; console.log(a[i]), 3000);
  }
  </code></pre>
  <p>// Output : [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined ]</p>