title: Javascript variable declaration within loop
body: <p>I have a habit that I am borderline compulsive about, but I think may be completely unnecessary. With code like:</p>
  
  <pre><code>function abc(){
    var a,b;
    for(var i=0;i&lt;10;i++){
      a=document.getElementsByTagName('LI').item(i).width;
      b=document.getElementsByTagName('DIV').item(i).width;
      // now do something with a and b
     }
     return;
  }
  </code></pre>
  
  <p>I am compulsive about declaring the variable before the loop as opposed to:</p>
  
  <pre><code>function abc(){
    for(var i=0;i&lt;10;i++){
     var a=document.getElementsByTagName('LI').item(i).width;
     var b=document.getElementsByTagName('DIV').item(i).width;
      // now do something with a and b
     }
     return;
  }
  </code></pre>
  
  <p>Notice that in the second code block, I define the variable with <code>var</code> each time the loop iterates. I imagine the first is best practice for readability, etc. But sometimes I'm just hacking something and don't need to follow best practice.</p>
  
  <p>My question is:</p>
  
  <p>Is there any reason <strong>not</strong> to define a variable that will be getting redefined using the <code>var</code> keyword inside a loop?</p>