forEach method in javascript

Hello friends, Today we are going to make you understand forEach method of javascript.

The forEach() method executes a callback function once for each and every element with in given array.

Syntax:

givenArray.forEach(callbackFunction(currentValue , index , array),thisArg);

callbackFunction:

Callback Function will be executed on each and every element of given array, it takes three arguments.

1) currentValue: The current element being processed in the array.

2) index: optional parameter, The index of the current element being processed in the array.

3) array: optional parameter, The array on which, forEach() method was called upon.

thisArg:

Optional parameter, Value to use as this when executing callback.

A). Before understand forEach method in more details lets iterate array elements in different ways.

OUTPUT:  Just open developer mode of browser by pressing F12 (Google Chrome Browser) key. Copy and paste script code in console panel. press return. You will get OUTPUT in browser.

1) Using array index:

var v = [12,23,34,45] document.write(v[0]+"<br />"); document.write(v[1]+"<br />"); document.write(v[2]+"<br />"); document.write(v[3]+"<br />");

2) Using for loop:

var v = [12,23,34,45,56,67,78,5,34,23,54,6,7,8,9,1,0,0,-2,1-45] for(var c=0; c<v.length; c++){ document.write(v[c]+"<br />"); }

3) Using while loop:

var v = [12,23,34,45,56,67,78,5,34,23,54,6,7,8,9,1,0,0,-2,1-45] var c=0; while(c<v.length){ document.write(v[c]+"<br />");   c++; }

4) Using do while loop:

var v = [12,23,34,45,56,67,78,5,34,23,54,6,7,8,9,1,0,0,-2,1-45] var c=0; do{ document.write(v[c]+"<br />");   c++; } while(c<v.length)

B). All possible ways to use forEach Method:

OUTPUT:  Just open developer mode of browser by pressing F12 (Google Chrome Browser) key. Copy and paste script code in console panel. press return. You will get OUTPUT in browser.

1) Passing method as an argument with declaration.

var v = [12,23,34,45,56,67,78,5,34,23,54,6,7,8,9,1,0,0,-2,1-45] v.forEach(function(e){ document.write(e+"<br />"); });

2) Passing arrow method as an argument with declaration.

var v = [12,23,34,45,56,67,78,5,34,23,54,6,7,8,9,1,0,0,-2,1-45] v.forEach(e=>{ document.write(e+"<br />"); });

3) Passing method as an argument with definition.

var v = [12,23,34,45,56,67,78,5,34,23,54,6,7,8,9,1,0,0,-2,1-45] var f = function(e,i,a){ document.write(e+"<br />"); } v.forEach(f);

4) Getting index of all array elements.

var v = [12,23,34,45,56,67,78,5,34,23,54,6,7,8,9,1,0,0,-2,1-45] var f = function(e,i,a){ document.write(i+"<br />"); } v.forEach(f);

5) Passing arrow method as an argument with definition.

var v = [12,23,34,45,56,67,78,5,34,23,54,6,7,8,9,1,0,0,-2,1-45] var f = (e,i,a)=>{ document.write(e+"<br />"); } v.forEach(f);

6) Getting index of all array elements.

var v = [12,23,34,45,56,67,78,5,34,23,54,6,7,8,9,1,0,0,-2,1-45] var f = (e,i,a)=>{ document.write(i+"<br />"); } v.forEach(f);

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.


Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.

 
 

{{c.Content}}

Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply


Categories