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.while
loop, that in most (if not all) benchmarks that I've seen is slower then thefor
orfor...in
alternatives. "My method" just removes the problems with the method presented in the post.Array.prototype
which is considered best practice.On a side note: you are using
var
inside thefor...in
declaration which might bring the wrath of Douglas Crockford upon you ;)