Good practices for writing for loops
In order to maintain local (function) scope, one should always var all variables not needed outside the given function. Thus the best way to write a generic for loop in JavaScript is:
for (var i=0; i < someValue; i++) { /* loop body */ }
For loops are often used to traverse the elements in an array, a collection or a string. For these cases the upper bound on the loop is determined by the length of the data being looped through. Generally one will want constant bounds rather than a changing .length if items are added to the collection during the execution of the loop. To keep track of the original length one could use a temporary variable to hold the length of the data as follows:
for (var i=0, len=someArray.length; i < len; i++) { /* loop body */ }
This enhances performance as well because it doesn't have to look up the length property of the object on every single iteration of the loop. Obviously if you need to keep track of the length as it varies during your loop execution, you would not use this method.