Table of Contents
Syntax and explanation
1) forEach methodThe forEach() method executes a provided function once for each element in an array. After executing the function for every array element, this method changes the values of the existing array elements according to the result of the provided function. Hence forEach() is a mutator method. Also, forEach method doesn’t return anything (undefined).
2) map methodThe map() method, similar to the forEach() method, executes the provided function once for each element in an array. But unlike the forEach() method, it creates a new array with the results of calling a function for every array element. Hence map() method relies on immutability. Also, map() does not execute/call the function for those array elements without values.
Both the methods take two arguments:
1) testFuncThe testFunc() is a function that is used to execute a condition on each element of the array until the function returns true, indicating that the element satisfying the condition was found.
The testFn() takes three arguments:
- currentValue: This indicates the current element in the array being processed.
- index: Indicates the index of the current element being processed.
- arr: This is the array that the method was called upon.
The method executes testFunc() for every element of the array and if true is returned by the testFunc().