Array.prototype.reduce() vs Array.prototype.forEach()
Array.prototype.reduce() and Array.prototype.forEach() are both JavaScript Array APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. Array.prototype.reduce(): Executes a reducer function on each element, resulting in a single output value. Its syntax is array.reduce(callbackFn, initialValue), taking callbackFn(accumulator, currentValue, index, array), and it returns the single value that results from the reduction. Array.prototype.forEach(): Executes a provided function once for each array element. Its syntax is array.forEach(callbackFn, thisArg), and it returns undefined. Example of Array.prototype.reduce(): [1, 2, 3].reduce((acc, x) => acc + x, 0) // 6. Example of Array.prototype.forEach(): [1, 2, 3].forEach(x => console.log(x)). Read the full references for Array.prototype.reduce() and Array.prototype.forEach() to see edge cases, time complexity, browser support, and the mistakes developers most often make with each. Other Array APIs worth comparing: Array.prototype.map(), Array.prototype.filter(), Array.prototype.find(), Array.prototype.findIndex(), Array.prototype.findLast(), Array.prototype.some().