4094

In Java, you can use afor loop to traverse objects in an array as follows:

String[] myStringArray = {"Hello", "World"};for (String s : myStringArray) {    // Do something}

Can I do the same in JavaScript?

Wolf's user avatar
Wolf
10.3k8 gold badges72 silver badges117 bronze badges
askedJun 10, 2010 at 0:04
Mark Szymanski's user avatar
10
  • 8
    Ok, so I'm a bit confused, it's ok to use the enhanced for loop when you are accessing the objects? And use a sequential one for filling one? Is this correct?CommentedJun 10, 2010 at 0:15
  • 56
    no, it's really simple, array objects have numeric indexes, so you want toiterate over those indexes in the numeric order, a sequential loop ensures that, theenhancedfor-in loopenumerates object properties, without an specific order, and it also enumerates inherited properties... foriterating over arrays sequential loops are always recommended...CommentedJun 10, 2010 at 0:38
  • 7
    related -stackoverflow.com/questions/5349425/…CommentedNov 1, 2011 at 17:53
  • 9
    jsben.ch/#/Q9oD5 <= Here a benchmark of a bunch of solutions for looping through arraysCommentedNov 3, 2016 at 19:45
  • 17
    @CMS No, it's not really simple. It's really simple in every other language. It's ridiculously complex in JS, where you havein andof that can both be used and do different things. Then you also haveforEach and the ugly and annoying index based looping. Every other modern language makes looping over a collection easy and straightforward with no surprises or confusion. JS could, too, but it doesn't.CommentedOct 5, 2018 at 15:59

46 Answers46

5285

Three main options:

  1. for (var i = 0; i < xs.length; i++) { console.log(xs[i]); }
  2. xs.forEach((x, i) => console.log(x));
  3. for (const x of xs) { console.log(x); }

Detailed examples are below.


1. Sequentialfor loop:

var myStringArray = ["Hello","World"];var arrayLength = myStringArray.length;for (var i = 0; i < arrayLength; i++) {    console.log(myStringArray[i]);    //Do something}

Pros

  • Works on every environment
  • You can usebreak andcontinue flow control statements

Cons

  • Too verbose
  • Imperative
  • Easy to haveoff-by-one errors (sometimes also called afence post error)

2.Array.prototype.forEach:

TheES5 specification introduced a lot of beneficial array methods. One of them, theArray.prototype.forEach, gave us a concise way to iterate over an array:

const array = ["one", "two", "three"]array.forEach(function (item, index) {  console.log(item, index);});

Being almost ten years as the time of writing that the ES5 specification was released (Dec. 2009), it has been implemented by nearly all modern engines in the desktop, server, and mobile environments, so it's safe to use them.

And with the ES6 arrow function syntax, it's even more succinct:

array.forEach(item => console.log(item));

Arrow functions are also widely implemented unless you plan to support ancient platforms (e.g.,Internet Explorer 11); you are also safe to go.

Pros

  • Very short and succinct.
  • Declarative

Cons

  • Cannot usebreak /continue

Normally, you can replace the need tobreak out of imperative loops by filtering the array elements before iterating them, for example:

array.filter(item => item.condition < 10)     .forEach(item => console.log(item))

Keep in mind if you are iterating an arrayto build another array from it, you should usemap. I've seen this anti-pattern so many times.

Anti-pattern:

const numbers = [1,2,3,4,5], doubled = [];numbers.forEach((n, i) => { doubled[i] = n * 2 });

Proper use case ofmap:

const numbers = [1,2,3,4,5];const doubled = numbers.map(n => n * 2);console.log(doubled);

Also, if you are trying toreduce the array to a value, for example, you want to sum an array of numbers, you should use thereduce method.

Anti-pattern:

const numbers = [1,2,3,4,5];const sum = 0;numbers.forEach(num => { sum += num });

Proper use ofreduce:

const numbers = [1,2,3,4,5];const sum = numbers.reduce((total, n) => total + n, 0);console.log(sum);

3. ES6for-of statement:

TheES6 standard introduces the concept of iterable objects and defines a new construct for traversing data, thefor...of statement.

This statement works for any kind of iterable object and also for generators (any object that has a\[Symbol.iterator\] property).

Array objects are by definition built-in iterables in ES6, so you can use this statement on them:

let colors = ['red', 'green', 'blue'];for (const color of colors){    console.log(color);}

Pros

  • It can iterate over a large variety of objects.
  • Can use normal flow control statements (break /continue).
  • Useful to iterate serially asynchronous values.

Cons

Do not usefor...in

@zipcodeman suggests the use of thefor...in statement, but for iterating arraysfor-in should be avoided, that statement is meant toenumerate object properties.

It shouldn't be used for array-like objects because:

  • The order of iteration is not guaranteed; the array indexes may not be visited in numeric order.
  • Inherited properties are also enumerated.

The second point is that it can give you a lot of problems, for example, if you extend theArray.prototype object to include a method there, that property will also be enumerated.

For example:

Array.prototype.foo = "foo!";var array = ['a', 'b', 'c'];for (var i in array) {    console.log(array[i]);}

The above code will console log "a", "b", "c", and "foo!".

That can be particularly a problem if you use some library that relies heavily on native prototypes augmentation (such asMooTools).

Thefor-in statement, as I said before, is there toenumerate object properties, for example:

var obj = {    "a": 1,    "b": 2,    "c": 3};for (var prop in obj) {    if (obj.hasOwnProperty(prop)) {        // or if (Object.prototype.hasOwnProperty.call(obj,prop)) for safety...        console.log("prop: " + prop + " value: " + obj[prop])    }}

In the above example, thehasOwnProperty method allows you to enumerate onlyown properties. That's it, only the properties that the object physically has, no inherited properties.

I would recommend you to read the following article:

user229044's user avatar
user229044
241k41 gold badges347 silver badges350 bronze badges
answeredJun 10, 2010 at 0:07
Christian C. Salvadó's user avatar
Sign up to request clarification or add additional context in comments.

13 Comments

I know this answer predates async and Promises, but I feel this is worth mentioning in any conversation pertaining to modern #"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach" rel="nofollow noreferrer">developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…)
con of es6for-of: can't get the current index
@Vicyou can, but it's not straightforward.
For reference, this is how to get the current index as you loop. I personally find this pretty straightforwards.for (const [i, x] of yourArray.entries()) { ... }.
For those looking at this answer and wondering whether they should choose forEach() of for-of, I would just recommend using for-of. for-of is the newer looping syntax that entirely replaces the need for forEach. There isn't anything that for-of offers that forEach does not, but the inverse isn't true.
|
1210
+50

Yes, assuming your implementation includes thefor...of feature introduced inECMAScript 2015 (the "Harmony" release)... which is a pretty safe assumption these days.

It works like this:

// REQUIRES ECMASCRIPT 2015+var s, myStringArray = ["Hello", "World"];for (s of myStringArray) {  // ... do something with s ...}

Or better yet, since ECMAScript 2015 also provides block-scoped variables:

// REQUIRES ECMASCRIPT 2015+const myStringArray = ["Hello", "World"];for (const s of myStringArray) {  // ... do something with s ...}// s is no longer defined here

(The variables is different on each iteration, but can still be declaredconst inside the loop body as long as it isn't modified there.)

A note on sparse arrays: an array in JavaScript may not actually store as many items as reported by itslength; that number is simply one greater than the highest index at which a value is stored. If the array holds fewer elements than indicated by its length, its said to besparse. For example, it's perfectly legitimate to have an array with items only at indexes 3, 12, and 247; thelength of such an array is 248, though it is only actually storing 3 values. If you try to access an item at any other index, the array will appear to have theundefined value there, but the array is nonetheless is distinct from one that actually hasundefined values stored. You can see this difference in a number of ways, for example in the way the Node REPL displays arrays:

> a              // array with only one item, at index 12[ <12 empty items>, 1 ]> a[0]           // appears to have undefined at index 0undefined> a[0]=undefined // but if we put an actual undefined thereundefined> a              // it now looks like this[ undefined, <11 empty items>, 1 ]

So when you want to "loop through" an array, you have a question to answer: do you want to loop over the full range indicated by its length and processundefineds for any missing elements, or do you only want to process the elements actually present? There are plenty of applications for both approaches; it just depends on what you're using the array for.

If you iterate over an array withfor..of, the body of the loop is executedlength times, and the loop control variable is set toundefined for any items not actually present in the array. Depending on the details of your "do something with" code, that behavior may be what you want, but if not, you should use a different approach.

Of course, some developers have no choice but to use a different approach anyway, because for whatever reason they're targeting a version of JavaScript that doesn't yet supportfor...of.

As long as your JavaScript implementation is compliant with theprevious edition of the ECMAScript specification (which rules out, for example, versions of Internet Explorer before 9), then you can use theArray#forEach iterator method instead of a loop. In that case, you pass a function to be called on each item in the array:

var myStringArray = [ "Hello", "World" ];myStringArray.forEach( function(s) {      // ... do something with s ...} );

You can of course use an arrow function if your implementation supports ES6+:

myStringArray.forEach( s => {      // ... do something with s ...} );

Unlikefor...of,.forEach only calls the function for elements that are actually present in the array. If passed our hypothetical array with three elements and a length of 248, it will only call the function three times, not 248 times. If this is how you want to handle sparse arrays,.forEach may be the way to go even if your interpreter supportsfor...of.

The final option, which works inall versions of JavaScript, isan explicit counting loop. You simply count from 0 up to one less than the length and use the counter as an index. The basic loop looks like this:

var i, s, myStringArray = [ "Hello", "World" ], len = myStringArray.length;for (i=0; i<len; ++i) {  s = myStringArray[i];  // ... do something with s ...}

One advantage of this approach is that you can choose how to handle sparse arrays. The above code will run the body of the loop the fulllength times, withs set toundefined for any missing elements, just likefor..of; if you instead want to handle only the actually-present elements of a sparse array, like.forEach, you can add a simplein test on the index:

var i, s, myStringArray = [ "Hello", "World" ], len = myStringArray.length;for (i=0; i<len; ++i) {  if (i in myStringArray) {    s = myStringArray[i];    // ... do something with s ...  }}

Depending on your implementation's optimizations, assigning the length value to the local variable (as opposed to including the fullmyStringArray.length expression in the loop condition) can make a significant difference in performance since it skips a property lookup each time through. You may see the length caching done in the loop initialization clause, like this:

var i, len, myStringArray = [ "Hello", "World" ];for (len = myStringArray.length, i=0; i<len; ++i) {

The explicit counting loop also means you have access to the index of each value, should you want it. The index is also passed as an extra parameter to the function you pass toforEach, so you can access it that way as well:

myStringArray.forEach( (s,i) => {   // ... do something with s and i ...});

for...of doesn't give you the index associated with each object, but as long as the object you're iterating over is actually an instance ofArray (and not one of the other iterable typesfor..of works on), you can use theArray#entries method to change it to an array of [index, item] pairs, and then iterate over that:

for (const [i, s] of myStringArray.entries()) {  // ... do something with s and i ...}

Thefor...in syntax mentioned by others is for looping over an object's properties; since an Array in JavaScript is just an object with numeric property names (and an automatically-updatedlength property), you can theoretically loop over an Array with it. But the problem is that it doesn't restrict itself to the numeric property values (remember that even methods are actually just properties whose value is a closure), nor is it guaranteed to iterate over those in numeric order. Therefore, thefor...in syntax shouldnot be used for looping through Arrays.

answeredApr 16, 2012 at 2:03
Mark Reed's user avatar

1 Comment

Note that some interpreters (e.g. V8) will automatically cache the length of the arrayif the code is called enough times and it detects that the length is not modified by the loop. While caching the length is still nice, it may not provide a speed boost when your code is being invoked enough times to actually make a difference.
453

You can usemap, which is a functional programming technique that's also available in other languages likePython andHaskell.

[1,2,3,4].map( function(item) {     alert(item);})

The general syntax is:

array.map(func)

In generalfunc would take one parameter, which is an item of the array. But in the case of JavaScript, it can take a second parameter which is the item's index, and a third parameter which is the array itself.

The return value ofarray.map is another array, so you can use it like this:

var x = [1,2,3,4].map( function(item) {return item * 10;});

And now x is[10,20,30,40].

You don't have to write the function inline. It could be a separate function.

var item_processor = function(item) {      // Do something complicated to an item}new_list = my_list.map(item_processor);

which would be sort-of equivalent to:

 for (item in my_list) {item_processor(item);}

Except you don't get thenew_list.

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredJun 10, 2010 at 0:09
hasen's user avatar

3 Comments

That particular example is probably better implemented usingArray.forEach.map is for generating a new array.
@hasen, theArray.prototype.map method is part of the ECMAScript 5th Edition Standard, is not yet available on all implementations (e.g. IE lacks of it), also foriterating over an array I think theArray.prototype.forEach method is moresemantically correct... also please don't suggest the for-in statement, see my answer for more details :)
@ChristianC.Salvadó This question now contains replies saying that ES5 cannot be relied upon and answers that use ES6 arrow notation. Are you even still on the platform? When did we get so old? 🥲
143

for (const s of myStringArray) {

(Directly answering your question: now you can!)

Most other answers are right, but they do not mention (as of this writing) thatECMAScript 6  2015 is bringing a new mechanism for doing iteration, thefor..of loop.

This new syntax is the most elegant way to iterate an array in JavaScript (as long you don't need the iteration index).

It currently works with Firefox 13+, Chrome 37+ and it does not natively work with other browsers (see browser compatibility below). Luckily we have JavaScript compilers (such asBabel) that allow us to use next-generation features today.

It also works on Node.js (I tested it on version 0.12.0).

Iterating an array

// You could also use "let" or "const" instead of "var" for block scope.for (var letter of ["a", "b", "c"]) {   console.log(letter);}

Iterating an array of objects

const band = [  {firstName : 'John', lastName: 'Lennon'},  {firstName : 'Paul', lastName: 'McCartney'}];for(const member of band){  console.log(member.firstName + ' ' + member.lastName);}

Iterating a generator:

(example extracted fromhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)

function* fibonacci() { // A generator function  let [prev, curr] = [1, 1];  while (true) {    [prev, curr] = [curr, prev + curr];    yield curr;  }}for (const n of fibonacci()) {  console.log(n);  // Truncate the sequence at 1000  if (n >= 1000) {    break;  }}

Compatibility table:http://kangax.github.io/compat-table/es6/#test-for..of_loops

Specification:http://wiki.ecmascript.org/doku.php?id=harmony:iterators

}

tagurit's user avatar
tagurit
5786 silver badges16 bronze badges
answeredAug 11, 2013 at 15:54
Marlon Bernardes's user avatar

Comments

133

In JavaScript it's not advisable to loop through an Array with a for-in loop, but it's better to use afor loop such as:

for(var i=0, len=myArray.length; i < len; i++){}

It's optimized as well ("caching" the array length). If you'd like to learn more,read my post on the subject.

Josh DeLong's user avatar
Josh DeLong
4757 silver badges27 bronze badges
answeredDec 7, 2010 at 7:24
sebarmeli's user avatar

Comments

116

6 different methods to loop through the array

You can loop through an array by many different methods. I have sorted my 6 favorite methods from top to bottom.

1. UsingJavaScript for loop

When it's to simply loop through an array, thefor loop is my first choice.

let array = [1, 2, 3, 4, 5];for (let i = 0; i < array.length; i++) {  console.log(array[i]);}

2. Using forEach loop

forEach loop is a modern way to loop through the array. Also, it gives more flexibility and control over the array and elements.

let array = [1, 2, 3, 4, 5];array.forEach((element) => {  console.log(element);});

3. Using for...of

for...of loop gives you direct access to the array elements.

let array = [1, 2, 3, 4, 5];for (let element of array) {  console.log(element);}

4. Using for...in loop

for...in gives you a key using which you can access array elements.

let array = [1, 2, 3, 4, 5];for(let index in array){  console.log(array[index]);}

5. Using while loop

while loop is can be used to loop through the array as well.

let array = [1, 2, 3, 4, 5];let length = array.length;while(length > 0){  console.log(array[array.length - length]);  length--;}

6. Using do...while loop

Likewise, I usedo...while loop

let array = [1, 2, 3, 4, 5];let length = array.length;do {  console.log(array[array.length - length]);  length--;}while (length > 0)

answeredJun 5, 2021 at 6:21
Satish Chandra Gupta's user avatar

Comments

94

Opera, Safari, Firefox and Chrome now all share a set of enhanced Array methods for optimizing many common loops.

You may not need all of them, but they can be very useful, or would be if every browser supported them.

Mozilla Labs published the algorithms they andWebKit both use, so that you can add them yourself.

filter returns an array of items that satisfy some condition or test.

every returns true if every array member passes the test.

some returns true if any pass the test.

forEach runs a function on each array member and doesn't return anything.

map is like forEach, but it returns an array of the results of the operation for each element.

These methods all take a function for their first argument and have an optional second argument, which is an object whose scope you want to impose on the array members as they loop through the function.

Ignore it until you need it.

indexOf andlastIndexOf find the appropriate position of the first or last element that matches its argument exactly.

(function(){    var p, ap= Array.prototype, p2={        filter: function(fun, scope){            var L= this.length, A= [], i= 0, val;            if(typeof fun== 'function'){                while(i< L){                    if(i in this){                        val= this[i];                        if(fun.call(scope, val, i, this)){                            A[A.length]= val;                        }                    }                    ++i;                }            }            return A;        },        every: function(fun, scope){            var L= this.length, i= 0;            if(typeof fun== 'function'){                while(i<L){                    if(i in this && !fun.call(scope, this[i], i, this))                        return false;                    ++i;                }                return true;            }            return null;        },        forEach: function(fun, scope){            var L= this.length, i= 0;            if(typeof fun== 'function'){                while(i< L){                    if(i in this){                        fun.call(scope, this[i], i, this);                    }                    ++i;                }            }            return this;        },        indexOf: function(what, i){            i= i || 0;            var L= this.length;            while(i< L){                if(this[i]=== what)                    return i;                ++i;            }            return -1;        },        lastIndexOf: function(what, i){            var L= this.length;            i= i || L-1;            if(isNaN(i) || i>= L)                i= L-1;            else                if(i< 0) i += L;            while(i> -1){                if(this[i]=== what)                    return i;                --i;            }            return -1;        },        map: function(fun, scope){            var L= this.length, A= Array(this.length), i= 0, val;            if(typeof fun== 'function'){                while(i< L){                    if(i in this){                        A[i]= fun.call(scope, this[i], i, this);                    }                    ++i;                }                return A;            }        },        some: function(fun, scope){            var i= 0, L= this.length;            if(typeof fun== 'function'){                while(i<L){                    if(i in this && fun.call(scope, this[i], i, this))                        return true;                    ++i;                }                return false;            }        }    }    for(p in p2){        if(!ap[p])            ap[p]= p2[p];    }    return true;})();
Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredJun 10, 2010 at 2:43
kennebec's user avatar

Comments

83

Introduction

Since my time in college, I've programmed in Java, JavaScript, Pascal,ABAP, PHP,Progress 4GL, C/C++ and possibly a few other languages I can't think of right now.

While they all have their own linguistic idiosyncrasies, each of these languages share many of the same basic concepts. Such concepts include procedures / functions,IF-statements,FOR-loops, andWHILE-loops.


A traditionalfor-loop

A traditionalfor loop has three components:

  1. The initialization: executed before the look block is executed the first time
  2. The condition: checks a condition every time before the loop block is executed, and quits the loop if false
  3. The afterthought: performed every time after the loop block is executed

These three components are separated from each other by a; symbol. Content for each of these three components is optional, which means that the following is the most minimalfor loop possible:

for (;;) {    // Do stuff}

Of course, you will need to include anif(condition === true) { break; } or anif(condition === true) { return; } somewhere inside thatfor-loop to get it to stop running.

Usually, though, the initialization is used to declare an index, the condition is used to compare that index with a minimum or maximum value, and the afterthought is used to increment the index:

for (var i = 0, length = 10; i < length; i++) {    console.log(i);}

Using a traditionalfor loop to loop through an array

The traditional way to loop through an array, is this:

for (var i = 0, length = myArray.length; i < length; i++) {    console.log(myArray[i]);}

Or, if you prefer to loop backwards, you do this:

for (var i = myArray.length - 1; i > -1; i--) {    console.log(myArray[i]);}

There are, however, many variations possible, like for example this one:

for (var key = 0, value = myArray[key], length = myArray.length; key < length; value = myArray[++key]) {    console.log(value);}

...or this one...

var i = 0, length = myArray.length;for (; i < length;) {    console.log(myArray[i]);    i++;}

...or this one:

var key = 0, value;for (; value = myArray[key++];){    console.log(value);}

Whichever works best is largely a matter of both personal taste and the specific use case you're implementing.

Note that each of these variations is supported by all browsers, including very very old ones!


Awhile loop

One alternative to afor loop is awhile loop. To loop through an array, you could do this:

var key = 0;while(value = myArray[key++]){    console.log(value);}

Like traditionalfor loops,while loops are supported by even the oldest of browsers.

Also, note that every while loop can be rewritten as afor loop. For example, thewhile loop hereabove behaves the exact same way as thisfor-loop:

for(var key = 0; value = myArray[key++];){    console.log(value);}

For...in andfor...of

In JavaScript, you can also do this:

for (i in myArray) {    console.log(myArray[i]);}

This should be used with care, however, as it doesn't behave the same as a traditionalfor loop in all cases, and there are potential side-effects that need to be considered. SeeWhy is using "for...in" for array iteration a bad idea? for more details.

As an alternative tofor...in, there's now also forfor...of. The following example shows the difference between afor...of loop and afor...in loop:

var myArray = [3, 5, 7];myArray.foo = "hello";for (var i in myArray) {  console.log(i); // logs 0, 1, 2, "foo"}for (var i of myArray) {  console.log(i); // logs 3, 5, 7}

Additionally, you need to consider that no version of Internet Explorer supportsfor...of (Edge 12+ does) and thatfor...in requires at least Internet Explorer 10.


Array.prototype.forEach()

An alternative tofor-loops isArray.prototype.forEach(), which uses the following syntax:

myArray.forEach(function(value, key, myArray) {    console.log(value);});

Array.prototype.forEach() is supported by all modern browsers, as well as Internet Explorer 9 and later.


Libraries

Finally, many utility libraries also have their ownforeach variation. AFAIK, the three most popular ones are these:

jQuery.each(), injQuery:

$.each(myArray, function(key, value) {    console.log(value);});

_.each(), inUnderscore.js:

_.each(myArray, function(value, key, myArray) {    console.log(value);});

_.forEach(), inLodash:

_.forEach(myArray, function(value, key) {    console.log(value);});
Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredFeb 29, 2016 at 18:56
John Slegers's user avatar

Comments

70

Use thewhile loop...

var i = 0, item, items = ['one', 'two', 'three'];while(item = items[i++]){    console.log(item);}

It logs: 'one', 'two', and 'three'

And for the reverse order, an even more efficient loop:

var items = ['one', 'two', 'three'], i = items.length;while(i--){    console.log(items[i]);}

It logs: 'three', 'two', and 'one'

Or the classicalfor loop:

var items = ['one', 'two', 'three']for(var i=0, l = items.length; i < l; i++){    console.log(items[i]);}

It logs: 'one','two','three'

Reference:Google Closure: How not to write JavaScript

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredJan 5, 2012 at 9:15
Timo Huovinen's user avatar

2 Comments

The first example of the "while" syntax won't work if any of the array elements is falsy.
... and this while loop is equivalent to: for (var i=0,item; item=items[i]; i++) , which takes away the need to declare the index and item variables beforehand...
41

If you want a terse way to write a fast loopand you can iterate in reverse:

for (var i=myArray.length;i--;){  var item=myArray[i];}

This has the benefit of caching the length (similar tofor (var i=0, len=myArray.length; i<len; ++i) and unlikefor (var i=0; i<myArray.length; ++i)) while being fewer characters to type.

There are even some times when you ought to iterate in reverse, such as when iterating over alive NodeList where you plan on removing items from the DOM during iteration.

falsarella's user avatar
falsarella
12.5k10 gold badges75 silver badges119 bronze badges
answeredJun 4, 2012 at 16:26
Phrogz's user avatar

2 Comments

For the people that don't get what is so ingenious: The i-- expression is first evaluated and allows the loop to continue when it's not falsish... Afterwards the counter is decremented. As soon as i becomes zero it will break out of the loop as zero is a falsish value in Javascript.
falsish? You mean falsey. Let's all stick the proper terminology to avoid confusion ;)
39

Some use cases of looping through an array inthe functional programming way in #"John", age: 21 }, { name: "Peter", age: 31 }, { name: "Andrew", age: 29 }, { name: "Thomas", age: 25 }];let sortByAge = people.sort(function (p1, p2) { return p1.age - p2.age;});console.log(sortByAge);

enter image description here

9. Find an element in an array

const people = [ {name: "john", age:23},                {name: "john", age:43},                {name: "jim", age:101},                {name: "bob", age:67} ];const john = people.find(person => person.name === 'john');console.log(john);

enter image description here

The Array.prototype.find() method returns the value of the first element in the array that satisfies the provided testing function.

References

answeredFeb 23, 2018 at 11:29
Yuci's user avatar

Comments

37

Yes, you can do the same in JavaScript using a loop, butnot limited to that. There are many ways to do a loop over arrays in JavaScript. Imagine you have this array below, and you'd like to do a loop over it:

var arr = [1, 2, 3, 4, 5];

These are the solutions:

1) For loop

Afor loop is a common way looping through arrays in JavaScript, but it is no considered as the fastest solutions for large arrays:

for (var i=0, l=arr.length; i<l; i++) {  console.log(arr[i]);}

2) While loop

A while loop is considered as the fastest way to loop through long arrays, but it is usually less used in the JavaScript code:

let i=0;while (arr.length>i) {    console.log(arr[i]);    i++;}

3) Do while
Ado while is doing the same thing aswhile with some syntax difference as below:

let i=0;do {  console.log(arr[i]);  i++;}while (arr.length>i);

These are the main ways to do JavaScript loops, but there are a few more ways to do that.

Also we use afor in loop for looping over objects in JavaScript.

Also look at themap(),filter(),reduce(), etc. functions on an Array in JavaScript. They may do things much faster and better than usingwhile andfor.

This is a good article if you like to learn more about the asynchronous functions over arrays in JavaScript.

Functional programming has been making quite a splash in thedevelopment world these days. And for good reason: Functionaltechniques can help you write more declarative code that is easier tounderstand at a glance, refactor, and test.

One of the cornerstones of functional programming is its special useof lists and list operations. And those things are exactly what thesound like they are: arrays of things, and the stuff you do to them.But the functional mindset treats them a bit differently than youmight expect.

This article will take a close look at what I like to call the "bigthree" list operations: map, filter, and reduce. Wrapping your headaround these three functions is an important step towards being ableto write clean functional code, and opens the doors to the vastlypowerful techniques of functional and reactive programming.

It also means you'll never have to write a for loop again.

Read more>>here:

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredMay 27, 2017 at 2:57
Alireza's user avatar

Comments

30

There is a way to do it where you have very little implicit scope in your loop and do away with extra variables.

var i = 0,     item;// Note this is weak to sparse arrays or falsey valuesfor ( ; item = myStringArray[i++] ; ){    item; // This is the string at the index.}

Or if you really want to get the id and have a really classicalfor loop:

var i = 0,    len = myStringArray.length; // Cache the lengthfor ( ; i < len ; i++ ){    myStringArray[i]; // Don't use this if you plan on changing the length of the array}

Modern browsers all support iterator methodsforEach,map,reduce,filter and a host of other methods on theArray prototype.

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredMay 16, 2011 at 22:52
Gabriel's user avatar

6 Comments

Note that some interpreters (e.g. V8) will automatically cache the length of the array if the code is called enough times and it detects that the length is not modified by the loop.
Thanks for the info @Phrogz it's true that there is a lot of optimizations that the VM can make, but since older browsers don't have this it would still be best practice to optimize for it since it is so cheap.
@Gabriel: Why? Please give real-world examples showing that not caching the length is actually a performance bottleneck. I follow the 'premature optimization is the root of all evil' approach. I will fix that one loop that actually poses a problem once I encounter it...
@StijndeWitt imo it is just a stylistic issue. Honestly I no longer even use for loops instead relying on underscore for things like _.each, _.map etc. to do these things. When I did write loops like this I cached the length primarily so that all my variable declaration were in one place, at the top of my function. Following my advice in this regard is inconsequential to any real world application. Premature optimization is super bad, but if optimization happens to result from stylistic decisions I don't think it actually matters.
@Gabriel I believe JavaScript already supports the map function on arrays, no need to introduce an additional lib for that.
|
29

There are various way to loop through array in JavaScript.

Generic loop:

var i;for (i = 0; i < substr.length; ++i) {    // Do something with `substr[i]`}

ES5's forEach:

substr.forEach(function(item) {    // Do something with `item`});

jQuery.each:

jQuery.each(substr, function(index, item) {    // Do something with `item` (or `this` is also `item` if you like)});

Have a lookthis for detailed information or you can also checkMDN for looping through an array in JavaScript & using jQuery checkjQuery for each.

answeredJul 23, 2014 at 12:59
RizN81's user avatar

Comments

28

Array loop:

for(var i = 0; i < things.length; i++){    var thing = things[i];    console.log(thing);}

Object loop:

for(var prop in obj){    var propValue = obj[prop];    console.log(propValue);}
Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredAug 1, 2016 at 21:18
bzim's user avatar

Comments

28

I would thoroughly recommend making use of theUnderscore.js library. It provides you with various functions that you can use to iterate over arrays/collections.

For instance:

_.each([1, 2, 3], function(num){ alert(num); });=> alerts each number in turn...
tagurit's user avatar
tagurit
5786 silver badges16 bronze badges
answeredApr 16, 2012 at 23:33
Andrew Thomson's user avatar

2 Comments

For new discoverers of this question, I'd just like to point outLo-Dash, a spiritual successor of Underscore's that improves upon it in many ways.
Why useunderscore ifECMA-262 has been added theforEach methor. The native code is always better.
27

If anybody is interested in the performance side of the multiple mechanisms available for Array iterations, I've prepared the following JSPerf tests:

https://jsperf.com/fastest-array-iterator

Performance results

Results:

The traditionalfor() iterator, is by far the fastest method, especially when used with thearray length cached.

let arr = [1,2,3,4,5];for(let i=0, size=arr.length; i<size; i++){    // Do something}

TheArray.prototype.forEach() and theArray.prototype.map() methods are the slowest approximations, probably as a consequence of thefunction call overhead.

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredAug 20, 2018 at 23:36
colxi's user avatar

4 Comments

is better usei = i +1 instead ofi++
Could be improved: Please use: ++i instead of i++, this will avoid an temporary object. So it reduces memory usage and cpu time (no allocation required)!
@PowerStat can you provide a link or reference about that ? I've never aheard about it, sounds interesting...
@colxi For such interesting things you should read the hardcore C++ stuff from Herb Sutter and Scott Meyers. The ++i vs i++ thing is from the book: Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions - I thing you could also find it ongotw.ca but can be proved for every programing language.
21

If you're using the jQuery library, consider usinghttp://api.jquery.com/jQuery.each/

From the documentation:

jQuery.each( collection, callback(indexInArray, valueOfElement) )

Returns:Object

Description:A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.

The$.each() function is not the same as$(selector).each(), which is used to iterate, exclusively, over a jQuery object. The$.each() function can be used to iterate over any collection, whether it is a map (JavaScript object) or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through thethis keyword, but Javascript will always wrap thethis value as anObject even if it is a simple string or number value.) The method returns its first argument, the object that was iterated.

falsarella's user avatar
falsarella
12.5k10 gold badges75 silver badges119 bronze badges
answeredOct 21, 2012 at 6:20
justingordon's user avatar

2 Comments

Agreed with Exception. Do not underestimate the impact of extra dependencies. I would advice against this except in code that is already heavily using jQuery anyway.
Update: These days, you can useArray.forEach to get much of the same effect with native arrays.
21

I did not yet see this variation, which I personally like the best:

Given an array:

var someArray = ["some", "example", "array"];

You can loop over it without ever accessing the length property:

for (var i=0, item; item=someArray[i]; i++) {  // item is "some", then "example", then "array"  // i is the index of item in the array  alert("someArray[" + i + "]: " + item);}

See this JsFiddle demonstrating that:http://jsfiddle.net/prvzk/

This only works for arrays that arenot sparse. Meaning that there actually is a value at each index in the array. However, I found that in practice I hardly ever use sparse arrays in JavaScript... In such cases it's usually a lot easier to use an object as a map/hashtable. If you do have a sparse array, and want to loop over 0 .. length-1, you need the for (var i=0; i<someArray.length; ++i) construct, but you still need anif inside the loop to check whether the element at the current index is actually defined.

Also, as CMS mentions in a comment below, you can only use this on arrays that don't contain any falsish values. The array of strings from the example works, but if you have empty strings, or numbers that are 0 or NaN, etc. the loop will break off prematurely. Again in practice this is hardly ever a problem for me, but it is something to keep in mind, which makes this a loop to think about before you use it... That may disqualify it for some people :)

What I like about this loop is:

  • It's short to write
  • No need to access (let alone cache) the length property
  • The item to access is automatically defined within the loopbody under the name you pick.
  • Combines very naturally with array.push and array.splice to use arrays like lists/stacks

The reason this works is that the array specification mandates that when you read an item from an index >= the array's length, it will return undefined. When you write to such a location it will actually update the length.

For me, this construct most closely emulates the Java 5 syntax that I love:

for (String item : someArray) {}

... with the added benefit of also knowing about the current index inside the loop

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredFeb 28, 2013 at 13:59
Stijn de Witt's user avatar

2 Comments

Notice that with this approach the loop will stop as soon it finds afalsey value, such as an empty string,0,false,NaN,null orundefined, even beforei reaches the length, e.g.:jsfiddle.net/prvzk/1
The loop condition could be(item=someArray[i]) !== undefined.
21

There are 4 ways of array iteration:

// 1: forfor (let i = 0; i < arr.length; ++i) {  console.log(arr[i]);}// 2: forEacharr.forEach((v, i) => console.log(v));// 3: for infor (let i in arr) {  console.log(arr[i]);}// 4: for offor (const v of arr) {  console.log(v);}

Summary: 1 and 3 solutions create extra variable, 2 - create extra function context.The best way is 4th - "for of".

answeredJun 15, 2021 at 14:03
Oleksandr Golovatyi's user avatar

2 Comments

do you care to elaborate why 4 "for of" is the best over the others
It doesn't create needless variables or function context. But if you don't care about small disadvantages you can use any of them, what is more comfortable for you. @YesItsMe Thank you for the question.
21

Esoteric

let a= ["Hello", "World"];while(a.length) { console.log( a.shift() ); }

Performance test

Today (2022-11-13) I perform a test on Chrome 107, Safari 15.2 and Firefox 106 on chosen solutions.

Conclusions

  • solutions C and D are fast or fastest on all browsers for all arrays.
  • solution A and B are slowest on all browsers for all arrays

Results

enter image description here

Details

I perform 3 tests:

  • small - for 2 elements array (like OP) - you can run ithere
  • medium - for 10K elements array and - you can run ithere
  • big - for 100K elements array - you can run ithere

The below snippet presents code used in the test.

function A(a) {  let r=0;  while(a.length) r+= a.shift().length;  return r;}function B(a) {  let r=0;  for(i in a) r+= a[i].length;  return r;}function C(a) {  let r=0;  for(x of a) r+= x.length;  return r;}function D(a) {  let r=0;  for (i=0; i<a.length; ++i) r+= a[i].length;  return r;}function E(a) {  let r=0;  a.forEach(x=> r+= x.length);  return r;}let arr= ["Hello", "World!"];[A,B,C,D,E].forEach(f => console.log(`${f.name}: ${f([...arr])}`))

Here are example results for Chrome for a medium array:

enter image description here

answeredDec 19, 2019 at 6:40
Kamil Kiełczewski's user avatar

10 Comments

that's the Haskell-y way to do it; keep taking the first one. clever, but probably slow.
@Sapphire_Brick actually it is quite fast - here istest
You have make a good point. I ran your example with an array of 1000 items, andwhile(a.length) { console.log(a.shift()); } was about twice as fast as thefor(var i = 0; i < a.length; i++) { console.log(a[i]); } version. ¯\_(ツ)_/¯
Even if it does not exist in your native language, you should not leave out articles in English (the indefinite article ("a" or "an") and the definite article ("the")). See e.g.English Articles - 3 Simple Rules To Fix Common Grammar Mistakes & Errors andA, AN, THE - Articles in English.
@Pitouli you are right - I rollback answer to its initial form. When I have more time then I will perform benchamarks again
|
18

There's a method to iterate over only own object properties, not including prototype's ones:

for (var i in array) if (array.hasOwnProperty(i)) {    // Do something with array[i]}

but it still will iterate over custom-defined properties.

In JavaScript any custom property could be assigned to any object, including an array.

If one wants to iterate over sparsed array,for (var i = 0; i < array.length; i++) if (i in array) orarray.forEach withes5shim should be used.

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredApr 15, 2012 at 12:50
kirilloid's user avatar

1 Comment

And how about usingfor (var i in array) if (++i) ?
15

The most elegant and fast way

var arr = [1, 2, 3, 1023, 1024];for (var value; value = arr.pop();) {    value + 1}

http://jsperf.com/native-loop-performance/8


Edited (because I was wrong)


Comparing methods for looping through an array of 100000 items and do a minimal operation with the new value each time.

Preparation:

<script src="//code.jquery.com/jquery-2.1.0.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script><script>    Benchmark.prototype.setup = function() {        // Fake function with minimal action on the value        var tmp = 0;        var process = function(value) {            tmp = value; // Hold a reference to the variable (prevent engine optimisation?)        };                // Declare the test Array        var arr = [];        for (var i = 0; i < 100000; i++)            arr[i] = i;    };</script>

Tests:

<a href="http://jsperf.com/native-loop-performance/16"    title="http://jsperf.com/native-loop-performance/16"><img src="http://i.imgur.com/YTrO68E.png" title="Hosted by imgur.com" /></a>
answeredMar 8, 2014 at 2:06
molokoloco's user avatar

4 Comments

This loop doesn't seem to follow order of items in the array.
My test was wrong. It's correct, showing all LOOPS now.jsperf.com/native-loop-performance/16
@bergi is right. This loop wipes out the array as it loops through it. Not what you want in most cases.
breaks on falsey items.
14

There are a couple of ways to do it in JavaScript. The first two examples are JavaScript samples. The third one makes use of a JavaScript library, that is, jQuery making use of the.each() function.

var myStringArray = ["hello", "World"];for(var i in myStringArray) {  alert(myStringArray[i]);}

var myStringArray = ["hello", "World"];for (var i=0; i < myStringArray.length; i++) {  alert(myStringArray[i]);}

var myStringArray = ["hello", "World"];$.each(myStringArray, function(index, value){  alert(value);})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredApr 21, 2016 at 16:03
Shubham Khatri's user avatar

1 Comment

for...in should be avoided for Array-like objects
14

The optimized approach is to cache the length of array and using the single variable pattern, initializing all variables with a singlevar keyword.

var i, max, myStringArray = ["Hello", "World"];for (i = 0, max = myStringArray.length; i < max; i++) {    alert(myStringArray[i]);    // Do something}

If the order of iteration does not matter then you should try reversed loop. It is the fastest as it reduces overhead condition testing and decrement is in one statement:

var i,myStringArray = ["item1","item2"];for (i =  myStringArray.length; i--) {    alert(myStringArray[i]);}

Or better and cleaner to use awhile loop:

var myStringArray = ["item1","item2"],i = myStringArray.length;while(i--) {   // Do something with fruits[i]}
Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredJan 11, 2014 at 20:53
Zaheer Ahmed's user avatar

Comments

13

In JavaScript, there are so many solutions to loop an array.

The code below are popular ones

/** Declare inputs */const items = ['Hello', 'World']/** Solution 1. Simple for */console.log('solution 1. simple for')for (let i = 0; i < items.length; i++) {  console.log(items[i])}console.log()console.log()/** Solution 2. Simple while */console.log('solution 2. simple while')let i = 0while (i < items.length) {  console.log(items[i++])}console.log()console.log()/** Solution 3. forEach*/console.log('solution 3. forEach')items.forEach(item => {  console.log(item)})console.log()console.log()/** Solution 4. for-of*/console.log('solution 4. for-of')for (const item of items) {  console.log(item)}console.log()console.log()

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredOct 14, 2016 at 10:31
Alongkorn's user avatar

Comments

13

The best way in my opinion is to use the Array.forEach function. If you cannot use that I would suggest to get the polyfill from MDN. To make it available, it is certainly the safest way to iterate over an array in JavaScript.

Array.prototype.forEach()

So as others has suggested, this is almost always what you want:

var numbers = [1,11,22,33,44,55,66,77,88,99,111];var sum = 0;numbers.forEach(function(n){  sum += n;});

This ensures that anything you need in the scope of processing the array stays within that scope, and that you are only processing the values of the array, not the object properties and other members, which is whatfor .. in does.

Using a regular C-stylefor loop works in most cases. It is just important to remember that everything within the loop shares its scope with the rest of your program, the { } does not create a new scope.

Hence:

var sum = 0;var numbers = [1,11,22,33,44,55,66,77,88,99,111];for(var i = 0; i<numbers.length; ++i){  sum += numbers[i];}alert(i);

will output "11" - which may or may not be what you want.

A working jsFiddle example:https://jsfiddle.net/workingClassHacker/pxpv2dh5/7/

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredOct 26, 2016 at 8:51
Espen's user avatar

Comments

12

If you want to use jQuery, it has a nice example in its documentation:

 $.each([ 52, 97 ], function( index, value ) {      alert( index + ": " + value ); });
answeredMar 30, 2016 at 2:37
red-o-alf's user avatar

Comments

12

It's not 100% identical, but similar:

   var myStringArray = ['Hello', 'World']; // The array uses [] not {}    for (var i in myStringArray) {        console.log(i + ' -> ' + myStringArray[i]); // i is the index/key, not the item    }

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredApr 18, 2012 at 14:46
Muhammad Alvin's user avatar

1 Comment

It seems that this would run up against similar problems as other for in usages with an array object, in that prototype member variables would be caught by the for in as well.
11

For example, I used in a Firefox console:

[].forEach.call(document.getElementsByTagName('pre'), function(e){    console.log(e);})

You can use querySelectorAll to get same result

document.querySelectorAll('pre').forEach( (e) => {    console.log(e.textContent);})
<pre>text 1</pre><pre>text 2</pre><pre>text 3</pre>

answeredOct 21, 2014 at 7:32
Joter's user avatar

Comments

Protected question. To answer this question, you need to have at least 10 reputation on this site (not counting theassociation bonus). The reputation requirement helps protect this question from spam and non-answer activity.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.