Array.reduce
Array.reduce
Function
Accumulate array elements from left to right.
Syntax
array.reduce(callback, initial)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| callback | Fn | No | Empty | The callback signature is fn(acc, value, index, current). |
| initial | Any | No | First element | Initial accumulated value. |
Return value
| Type | Description |
|---|---|
| Any | Returns the final cumulative value. |
Example
items = [1, 2, 3] result = items.reduce(fn(acc, value) { acc + value }, 0) // Output: 6 print result
Notes
- An error will be reported when initial is not provided for an empty array.