Anjan Dutta
Reduce in JavaScript
Reduce in JavaScript
Published On: 27/10/2021
JavaScript reduce
method works only on arrays and it returns a single value as the result.
It accepts a callback function as the parameter. The callback function is also called a reducer function.
The reduce
method executes the reducer
function on each element of the array and it passes the return value of the previous operation as the first parameter of the reducer function.
See the below example. It's a function to calculate the sum of all the elements in an array.
const number_list = [17, 20, 13];const sum = number_list.reduce((previousValue, previousValue) => { return previousValue + previousValue; });console.log(sum);
This is the simplest example of the JavaScript reduce
funtion.