Joined September 2012
·

Shachar Haad

Israel
·
·
·

@dayjo

  • I do prefer your implementation, and it used to be my preferred method for iterating through an array prior to forEach, map, and other ES5 methods.
  • My comment was in regards to the use of while loop, that in most (if not all) benchmarks that I've seen is slower then the for or for...in alternatives. "My method" just removes the problems with the method presented in the post.
  • In your example, you are missing a condition to filter out extensions to Array.prototype which is considered best practice.

On a side note: you are using var inside the for...in declaration which might bring the wrath of Douglas Crockford upon you ;)

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] );
}
Achievements
1 Karma
0 Total ProTip Views
Interests & Skills