-5

In JavaScript I am aware of a number of different ways to iterate over each element in an array. At present my favorite is the array.forEach(function(entry){}) style but I know there is also the traditional for (var i = 0; i < array.length; i++) as well as for (entry in array) and JQuery's $.each() method.

My question is this: given a list of variable length eg: var arr = ['abc', 'xyz', '123', ...] what's the best way to loop over each element?

nettux
  • 5,270
  • 2
  • 23
  • 33
  • 2
    Don't use `for (entry in array)` for arrays. It's specifically for iterating over objects and you can get into trouble if you use it for arrays. – Andy May 02 '14 at 13:17
  • 1
    Note that `array.forEach()` is not available in older browsers (IE8 and below, Firefox before 1.5) – Paul Roub May 02 '14 at 13:17
  • With jQuery you obviously have the added overhead of adding in jQuery. I prefer a tradition `for-loop`. – Andy May 02 '14 at 13:18
  • 1
    [relevant reading](http://stackoverflow.com/q/9329446/497418) – zzzzBov May 02 '14 at 13:18
  • nobody remembers of `while`? cause the for is based on `while` and `foreach` on `for`....so instead to do 2-3 operations you can do just 1 and buy some extra performance – HellBaby May 02 '14 at 13:19
  • 1
    Check out [this link](http://www.sebarmeli.com/blog/2010/12/06/best-way-to-loop-through-an-array-in-javascript/). Very informative – Dumisani May 02 '14 at 13:20
  • http://jsperf.com/123453svfs – Albzi May 02 '14 at 13:22
  • by my experience , the best is : for (var i = 0; i < 5; i++) because you write your functions and if some day you want to merge or rewrite your functions in another language like java, C++ or php you 'll have just a little changes. unlike jquery has his own language. – Mimouni May 02 '14 at 13:22
  • Answering your new question: You can use the `for(var i=0; i<5; i++)` for read your array or `for (var i = 0; i < array.length; i++)` – Jabel Márquez May 02 '14 at 13:46

3 Answers3

4

For performance I would favor:

for (var i = 0; i < 5; i++)

For ease of use I generally use:

array.forEach(function(entry){})

Or for DOM elements I prefer jQuery: $.each()

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • What makes for (var i = 0; i < 5; i++) better for performance? – nettux May 02 '14 at 13:49
  • @nettux443: When you use other objects to iterate over them, like jQuery, you will always have performance loss. `for` is just the most bare metal way of iterating. – Patrick Hofman May 02 '14 at 13:50
1

Is not better one than other.

The for each loops are when you don't know how many items are in the array. array.forEach(function(entry){}) or $.each()

The basic loop (for) is when you know the especific number of items in the array. for (var i = 0; i < 5; i++). And this is less load for the process.

Jabel Márquez
  • 772
  • 1
  • 11
  • 38
  • you can say for (var i = 0; i < array.length; i++). foreach is great if you don't have an easy way to know how many items are in a collection. – Nzall May 02 '14 at 13:32
0

for (entry in array) is the one that is the most different in that it is a way to iterate over properties of an object, and not items in a list. It's not like a traditional foreach

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

TGH
  • 38,769
  • 12
  • 102
  • 135