Easy Tutorial
❮ Prop Url Maxlength Jsref Utc ❯

JavaScript reduce() Method

JavaScript Array Object

Example

Calculate the sum of array elements:

Output result:

125

Definition and Usage

The reduce() method executes a reducer function on each element of the array, resulting in a single output value.

reduce() can be used as a higher-order function for function composition.

Note: reduce() does not execute the callback function for empty arrays.


Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method Chrome IE Firefox Safari Opera
reduce() Yes 9.0 3.0 4 10.5

Syntax

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

Parameters

Parameter Description
function(total, currentValue, index, arr) Required. A function to be executed on each element in the array. <br>Function parameters: <br>total - Required. The initial value, or the previously returned value of the function. <br>currentValue - Required. The current element. <br>currentIndex - Optional. The index of the current element. <br>arr - Optional. The array object the current element belongs to.
initialValue Optional. A value to be passed to the function as the initial value.

Technical Details

Return Value: Returns the accumulated result from the last call of the callback function.
JavaScript Version: ECMAScript 3

More Examples

Example

Calculate the sum of array elements after rounding:


JavaScript Array Object

❮ Prop Url Maxlength Jsref Utc ❯