forEach() Array Method

JavaScript provide us with several built in functions to work with arrays which are know as Array Methods. Lets take a closer look at JavaScript forEach() method.

The forEach() method executes a callback function for each element of array. That callb…


This content originally appeared on DEV Community and was authored by Ali Taha Shakir

JavaScript provide us with several built in functions to work with arrays which are know as Array Methods. Lets take a closer look at JavaScript forEach() method.

The forEach() method executes a callback function for each element of array. That callback function accepts between one and three arguments:

  • Current Value (required) – The value of the current array element being processed in loop
  • Index (optional) – The current element’s index number
  • Array (optional) – The array forEach() was called upon

Considering that we have the following array below:

const numbersArray = [1, 2, 3, 4, 5];
Enter fullscreen mode Exit fullscreen mode

Lets apply forEach() method to numbers array, you need a callback function (or anonymous function):

numbersArray.forEach(function() {
    // code
});
Enter fullscreen mode Exit fullscreen mode

The function will be executed on each element of the array. It requires the current value parameter which represents the element of an array which is currently being processed in loop:

numbersArray.forEach(function(number) {
    console.log(number);
});
Enter fullscreen mode Exit fullscreen mode

Alt Text

This is the minimum required syntax to run forEach() method.

Alternatively, you can use the ES6 arrow function representation for simplifying the code:

numbersArray.forEach(number => console.log(number));
Enter fullscreen mode Exit fullscreen mode


This content originally appeared on DEV Community and was authored by Ali Taha Shakir


Print Share Comment Cite Upload Translate Updates
APA

Ali Taha Shakir | Sciencx (2021-02-15T15:44:26+00:00) forEach() Array Method. Retrieved from https://www.scien.cx/2021/02/15/foreach-array-method/

MLA
" » forEach() Array Method." Ali Taha Shakir | Sciencx - Monday February 15, 2021, https://www.scien.cx/2021/02/15/foreach-array-method/
HARVARD
Ali Taha Shakir | Sciencx Monday February 15, 2021 » forEach() Array Method., viewed ,<https://www.scien.cx/2021/02/15/foreach-array-method/>
VANCOUVER
Ali Taha Shakir | Sciencx - » forEach() Array Method. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/15/foreach-array-method/
CHICAGO
" » forEach() Array Method." Ali Taha Shakir | Sciencx - Accessed . https://www.scien.cx/2021/02/15/foreach-array-method/
IEEE
" » forEach() Array Method." Ali Taha Shakir | Sciencx [Online]. Available: https://www.scien.cx/2021/02/15/foreach-array-method/. [Accessed: ]
rf:citation
» forEach() Array Method | Ali Taha Shakir | Sciencx | https://www.scien.cx/2021/02/15/foreach-array-method/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.