

callbackFn is always called with undefined as this, which gets substituted with globalThis if callbackFn is non-strict. Unlike other iterative methods, reduce() does not accept a thisArg argument. It is not invoked for empty slots in sparse arrays. The final value of accumulator (which is the value returned from callbackFn on the final iteration of the array) becomes the return value of reduce().ĬallbackFn is invoked only for array indexes which have assigned values. Every time, the return value of callbackFn is passed into callbackFn again on next invocation as accumulator. It runs a "reducer" callback function over all elements in the array, in ascending-index order, and accumulates them into a single value.

The reduce() method is an iterative method. In this case, if the array is empty (so that there's no first value to return as accumulator), an error is thrown. If initialValue is not specified, accumulator is initialized to the first value in the array, and callbackFn starts executing with the second value in the array as currentValue. If initialValue is specified, callbackFn starts executing with the first value in the array as currentValue. initialValue OptionalĪ value to which accumulator is initialized the first time the callback is called. On the first call, its value is 0 if initialValue is specified, otherwise 1. The index position of currentValue in the array. On the first call, its value is array if initialValue is specified otherwise its value is array. On the first call, its value is initialValue if the latter is specified otherwise its value is array. The value resulting from the previous call to callbackFn. The function is called with the following arguments: accumulator For the last invocation, the return value becomes the return value of reduce(). Its return value becomes the value of the accumulator parameter on the next invocation of callbackFn. ()Ī function to execute for each element in the array.Object.prototype._lookupSetter_() Deprecated.Object.prototype._lookupGetter_() Deprecated.


Object.prototype._defineGetter_() Deprecated.
