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];
Lets apply forEach() method to numbers array, you need a callback function (or anonymous function):
numbersArray.forEach(function() {
// code
});
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);
});
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));
This content originally appeared on DEV Community and was authored by Ali Taha Shakir

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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.