JavaScript array iterator methods accept a function as their first argument and invoke that function once for each element (or some elements) of the array.
Predicate Function
In this post you will come across a term called predicate function, which is defined as a function that either returns true or false.
forEach
forEach() method modifies the original array.
The function you pass as the argument to forEach() method takes in three arguments: value of the array element, the index of the array element and the array itself. Most often, you will need only one argument - the value of the array element.
The every() and some() methods apply a specified predicate function to the elements of the array, then return true or false.
The every() method returns true if and only if the predicate function returns true for all elements in the array.
The some() method returns true if there exists at least one element in the array for which the predicate returns true and returns false if and only if the predicate returns false for all elements of the array.
The find() and findIndex() methods stop iterating the first time the predicate finds an element. When that happens, find() returns the matching element, and findIndex() returns the index of the matching element.
If no matching element is found, find() returns undefined and findIndex() returns -1.
letname= ["keith", "lemon", "keith", "lemon"]constnameFound=name.find(name=>name.length===5)console.log(nameFound) // keith
reduce
reduce() combines the elements of an array to produce a single value, using a specified function.
reduce() takes two arguments. The first is the callback function that takes two parameters (accumulator, currentValue) as arguments.
On each iteration, accumulator is the value returned by the last iteration, and the currentValue is the current element. The second argument (optional) is an initial value passed to the function.