So there’s a few things going on here that seem to have you confused. And that’s not unusual, there’s a lot going on in this lesson.
First, the spread operator in the parameter turns any number of variables passed, into an array. By doing so, we can use array methods on them. There are a number of array methods we’ve learned at this point, things like .map
and .filter
, and .reduce
is simply another array method.
If you haven’t yet, it might be a great idea to take a look at the docs on Array.reduce, either on MDN or on devdocs.io - it’s easy to get a little twisted when it comes to reduce.
So the signature for reduce is a little different. Where map and filter take a callback function, reduce takes two parameters: callback and initial_state. so that , 0
you’re asking about? that’s saying that, initially, our accumulator is zero.
Now, the callback being passed into reduce. It has been collapsed, a lot, but let’s expand it out and see if it makes a little more sense. I will change the variable names, but for the sake of reduce, when you see accumulator
, think a
from your function, and when you see member
, think b
.
return args.reduce(function(accumulator, member){
return accumulator+member; // add the current value to our accumulator, each time through
}, 0 /* sets accumulator to zero on the first pass */)
I hate the collapsed form, using a
and b
and m
for variable names. Saves keystrokes but LOUSY for self-documenting code. From the above, you can see more of what’s happening. Sane variable names.
That is, aside from variable names, exactly the same code as in your original, simply expanded out for readability. Either the fat-arrow function, or the traditional one, would work fine in this context.
Hope that helps!