Cache length of array in a for-loop

Cache length of array in a for-loop

by Nirvanachain

HTML

<!-- Helpful Article:
http://net.tutsplus.com/tutorials/javascript-ajax/the-essentials-of-writing-high-quality-javascript/?utm_source=javascriptweekly&utm_medium=email
-->

JavaScript

//cache the length of the array (or collection) you’re iterating over in a variable.  In this example the .length of the array is cached in the 'max' variable.
(function () {
    'use strict';

    var i = 0,
        myArray = ['red', 'yellow', 'green'],
        max;

    for (i = 0, max  = myArray.length; i < max; i++) {
        console.log(myArray[i]);
    }
    
}());