Is forEach method designed to work for only array in javascript?Can anyone share idea or an example on what major task can be completed with forEach() method or is it useful for only getting element value and index?
-
1https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/forEach - would be the best start for you – Henrik Andersson Apr 15 '13 at 09:10
3 Answers
No.
It is designed to work on arrays, but can operate on any array-like object (although you still have to access it from somewhere where it exists).
For example, on a NodeList:
Array.prototype.forEach.call(
document.getElementsByTagName('div'),
function (divElement) { console.log(divElement); }
);

- 914,110
- 126
- 1,211
- 1,335
-
1Try `"abc".forEach(function(v){console.log(v)})`. You'd have to use apply or call. – Denys Séguret Apr 15 '13 at 09:11
-
@dystroy — Yes, you have to use apply or call. That doesn't mean you can't use it. – Quentin Apr 15 '13 at 09:13
-
@Quentin I agree (and I didn't downvote). I simply think this should be clearer. – Denys Séguret Apr 15 '13 at 09:14
-
@Quentin i agree too, with the use of call and apply it works but is it only for Nodelist? – Maizere Pathak.Nepal Apr 15 '13 at 09:16
-
1@Maizere - No, _any_ array-like object, e.g., it would work on this object: `{"0":10,"1":20,"length":2}`. – nnnnnn Apr 15 '13 at 09:18
-
@nnnnnn is the use of call and apply compulsory for arry-like objects – Maizere Pathak.Nepal Apr 15 '13 at 09:23
-
@Maizere — No, but you have to do something to set `this` (inside the `forEach` function) to be the array-like object you want to operate on. – Quentin Apr 15 '13 at 09:24
-
-
@Quentin since it is designed invoke func for the element of an array how can it invoke function for the object with string key – Maizere Pathak.Nepal Apr 15 '13 at 09:33
-
-
@nnnnnn forEach() method doesn't work for the object u mentioned – Maizere Pathak.Nepal Jun 19 '13 at 11:55
It is an Array function. See documentation
You can use it on any array-like object but it's not very convenient because you have to use apply
or call
. For example :
[].forEach.call("abc",function(v){console.log(v)})
To iterate over objects keys, you should use for..in :
for (key in object) {
var value = object[key];
}
Note that jQuery's compatibility function $.each enables the same iteration function for both arrays and non array objects but you generally know if you're dealing with an array (or you should) so I'd use it only for compatibility with old browsers.

- 372,613
- 87
- 782
- 758
-
Just for good reading as well! http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea – Henrik Andersson Apr 15 '13 at 09:12
The forEach method in JavaScript allows you to traverse an array and apply a particular action to each of its elements through a function. Let's see an example:
array.forEach(function(elementoActual, indice, array))
elmentoActual: es el valor del elemento actual del array. índice: es el índice del elemento actual del array (opcional). array: es el array que se está recorriendo (opcional).
For more information I leave the following article https://4geeks.com/es/how-to/metodo-foreach-javascript