What should be the name of the arguments in Array.reduce?

As a matter of best practices in terms of code readability, Which of the following naming convention should be used while naming arguments of the callback in Array.reduce?

const studentAges= [15,16,14,15,14,20]

  • Generic

    const sum = studentAges.reduce((accumulator,currentValue)=> accumulator +=currentValue,0);

  • Specific

    const sum = studentAges.reduce((sum,age)=> sum+=age,0);

Please refer to the documentation on the reduce parameters and their order.

First param is the accumulator followed by currentValue, currentIndexOptional and array.

As far as the naming convention it is up to you and whatever coding standards you follow or prefer.

I personally prefer what you call the generic approach since it is consistent between the uses ... but again totally personal preference :)

JavaScript Array reduce() Method, (callback);// Optionally, you can specify an initial valuelet result = arr. However, if you consider in that definition the accumulator is the result array, you may understand the Array.reduce method as something with a semantic very close to the one of Array.map or Array, filter. The difference is, with Array.reduce, the result array is explicitly passed as an argument and maintained by

You can call it accumulator, but as the final value of the accumulator is the result of the reduce function, you can also call it result, which makes it more clear that we are looking at the resultant value. i prefer result as that helps make that point otherwise you have to remember that ... the accumulator is what gets returned. you can of course rename the result to whatever you are looking for out of the function too. So guess it depends on what makes it more readable. my pref though is result, because it also gets the result of each iteration.

Javascript reduce() on Object, the value of the total parameter when the reduction starts. Can somebody explain to me how Array.reduce can put functions as arguments in function composition like this: const composeB = (f, g) => x => f(g(x)) const add = a => b => a + b const add5 = add(5) const double = a => a * 2 const add5ThenDouble = [double, add5].reduce(composeB) console.log(add5ThenDouble(6)); // 22

I guess it is a matter of preference but I would go for Generic

const sum = studentAges.reduce((item,accumulator)=> accumulator +=item,0);

as there is no need here to be specific. You are passing a callback as argument, and it is the way you use the arguments more than the arguments themselves that makes the callback specific to your usage.

Learn & Understand JavaScript's Reduce Function, In this article, we will look a the Array.prototype.reduce() function. 0 as the starting point ( which is the argument given to the first function called in our array ). first called with ('', 'r') => returns 'r' the empty string '' comes from the 2nd argument to reduce, and the 'r' is the first element of the array then ('r', 'e') => returns 're' the ‘r’ comes from the previous return value, and ‘e’ is the next element of the array

A Guide To The Reduce Method In Javascript​, The reduce method cycles through each number in the array much like it The way to do this is by taking advantage of the other arguments in the reduce On our first pass, we want the name of the first key to be our current  The second parameter is where each value of the array is passed during each step of the reduction. The return value of the callback becomes the new value of the accumulator. When the array is exhausted, array_reduce() returns accumulated value.

Array.prototype.reduce(), Syntax arr.reduce(callback[, initialValue]) Parameters callback Function… is a function that will be called once for every item in the array. To handle multiple array parameters, you can use arguments. Thanks to that, your function can take N parameters, it is more generic. Then, you can flat all your array parameters, and start to reduce your data. When you will reduce our array, you will create a new array by excluding redundant data.

A look at the Array.reduce() function, The Array type provides a method called reduce() that helps you to reduce an If you pass in the initialValue argument, the reduce() method will assign it to the  Instead of variable arguments you should just expect a Seq as an argument to a function. It's very easy to create List or Array in F# and you can directly inline those creations with a function call. You don't get problems if you already have a collection.

Comments
  • .reduce((item,accumulator) No, the accumulator is the first argument to the reduce callback. Also, your second code doesn't have any item defined
  • @CertainPerformance edits made in the question.
  • Specific is better
  • The question is opinion-based, but I think being as specific as you can is best for readability. So, for example, your sum in the second code's reduce isn't actually the sum, it's only a partial sum of the ages so far. I think I would use: const studentAgeSum = studentAges.reduce((studentAgesSubtotal, studentAge)=> studentAgesSubtotal + studentAge, 0); If you see studentAgesSubtotal, you immediately know what that means. If you see accumulator, you have no idea. If you see sum, you only have some idea.
  • @CertainPerformance you have a valid point in favor of specific approach, however, using generic one can be preferred when you want to keep things consistent throughout the code, anyone who knows about reduce would know what accumulator means.Thanks for your input though
  • That was a mistake that I have corrected now, your answer makes sense to me, thanks.