Stop writing for loops
28 Sep 2015Use the native forEach function instead.
Example:
var numbers = [1,9,9,2];
for(var i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
// is equivalent to
numbers.forEach(function(value, index, array) {
/*via this callback function
you still have access to the whole array
and the index of the current value.*/
console.log(value);
});
Advantages:
.forEach()
is easier to understand for a noncoder.- Faster to type.