Joined September 2012
·
Posted to
Neat way to loop through an array in JS
over 1 year
ago
As noted previously, modifying the array you're working with, could raise issues.
For while loops, I prefer:
var arr = ['first', 'second', 'third'].reverse();
var index = arr.length;
while ( index-- ) {
console.log( arr[index] );
}
@dayjo
forEach,map, and other ES5 methods.whileloop, that in most (if not all) benchmarks that I've seen is slower then thefororfor...inalternatives. "My method" just removes the problems with the method presented in the post.Array.prototypewhich is considered best practice.On a side note: you are using
varinside thefor...indeclaration which might bring the wrath of Douglas Crockford upon you ;)