Explain me mechanism of reduce()? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Explain me mechanism of reduce()?

let list = [1, 2, 3, 4]; let ans = list.reduce(function (a, b) { return (a + b); }, 5); console.log(ans);

6th Sep 2018, 7:37 PM
pro
pro - avatar
1 Answer
+ 2
The job of the reduce function is to "reduce" an array into a single value (e.g. sum of all it's values) it takes in a function, that function should have a first parameter which is the accumulator (the single value that gets eventually returned) and a second one which is current array element, there are 2 other the link mentions. Get more information here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
6th Sep 2018, 7:45 PM
TurtleShell
TurtleShell - avatar