forEach() method calls a function once for each element in an array.
syntax ↴
forEach(callbackFn) A function to execute for each element in the array ↴
function is called with the following arguments ↴
forEach(function(element, index, array), thisArg) ↴
callback function allows access to each element, the index, and the original array itself.
thisArg (optional). Value to use as this when executing callbackFn. By default, it is undefined.
forEach() method allows us to loop through an array of any type of item.
forEach() always returns undefined and therefore it is not chainable like other iterative methods.
forEach() method does not create a new array.
forEach() method does not execute the function for empty elements.
If you want to return a new array containing the results of calling a function on every array element, use the map() method.
forEach() method has no stop or break.
forEach() expects a synchronous function - it does not wait for promises.
forEach() method is used to execute a provided function once for each array element. It does not return a new array, and it does not change the original array. It is mainly used to perform side effects and update state.
forEach() method does not mutate the array on which it is called, however, a callback function may do so.
forEach() similar to iterating over an array using a for loop ↴
const myArray = [1, 4, 9, 16];
for (let i = 0; i < myArray.length; i++) {
myArray[i] = myArray[i] * 2;
console.log(myArray[i]);} returns ↴
2
8
18
32
myArray returns ↴
[2, 8, 18, 32]
myArray.forEach(callbackFn)