Javascript .reduce method

Learning to use the methods in Javascript has been more challenging than I expected. It seemed that they were a way to save the time in creating the method yourself. Which is true, as long as you fully understand the method, it’s output, and how the callback function works.

One example that has been recently perplexing for me is the array method reduce.

let num = arr.reduce(accumulator, currentValue) => accumulator + currentValue;

This function will reduce an entire array to a single value, and return that value, based on operation that the callback function performs. I ran into trouble when I tried to reduce values from an object using the array values as keys. It turns out that an initial value can be assigned to the accumulator. If nothing is assigned the default starting value of the accumulator will be the first element of the array. In my situation I was adding a different type of value from an object. I needed the accumulator to start at zero.

let num = arr.reduce(callbackFunct(), 0);

I’m learning to read the documentation carefully! I’ve spent loads of time troubleshooting issues that were caused by my lack of understanding on specific methods.