title: Scope of variable in javascript
body: <p>I have some javascript code that resembles this:</p>
  
  <pre><code>for (i = 0; i &lt; numTimes; i++) {
     DoStuff();
  }
  
  
  function DoStuff() {
  
  for (i = 0; i &lt; 100; i++) {
  
    console.log(i);
  
  }
  
  }
  </code></pre>
  
  <p>I am finding that the second time the <code>DoStuff()</code> is called, the value of <code>i</code> in the loop starts with 1. I assume this is due to the way the scoping of variables work in JS. Other than changing the variable name in the <code>DoStuff()</code> function, what's the cleanest way of resolving this and can someone explain this behavior?</p>
  
  <p><strong>EDIT: Thanks for the responses. It appears that JS has "lexical scope" instead of "block scope". Is this what I am seeing here? Can someone explain what lexical scope is in newbie terms?</strong></p>