/

HTML / CSS / JavaScript Tutorial

JavaScript Array method: reduce()

[this page | pdf | back links]

The reduce() method (when applied to a JavaScript array) reduces the values of an array to a single value (from left to right, i.e. from lowest to highest index).

 

It has the following syntax with the following parameters. It returns the accumulated result from the last call of the function. It does not execute the function if the array element does not have a value.

 

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

 

Parameter

Required / Optional

Description

function(total, currentValue, index, arr)

Required

A function to be run for each element

initialValue

Optional

A value to be passed to the function to be used as the initial value

 

The function arguments are:

 

Parameter

Required / Optional

Description

total

Required

The initalValue or the previously returned value of the function

currentValue

Required

The value of the current element

Index

Optional

The array index of the current element

arr

Optional

The array object which the current element belongs to

 


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Arrays


Desktop view | Switch to Mobile