reduce() in JavaScript
The reduce() method is used to iterate over an array and reduce it to a single
value. This value can be a sum, product, object, or even another array.
Syntax
[Link](callback, initialValue)
Parameters
callback(accumulator, currentValue, index, array)
accumulator → Stores the accumulated result.
currentValue → The current element being processed.
index (optional) → The current index.
array (optional) → The original array.
initialValue (optional)
The initial value of the accumulator.
If not provided, the first element is used as the initial value.
Example 1: Sum of an Array
const numbers = [1, 2, 3, 4, 5];
const sum = [Link]((acc, num) => acc + num, 0);
[Link](sum); // Output: 15