Skip to main content

Command Palette

Search for a command to run...

On JavaScript reduce method

Updated
2 min readView as Markdown
C

I create stunning user-friendly web apps that engage and simplify the user experience on the client side. I love the intersection of solving customer problems while making their journey seamless. My current focus is building modular AI architectures using n8n, which allows me to deploy private, high-speed automation on self-hosted VPS environments.

I am also a technical writer. I share my journey through articles on new technologies, tutorials, and walkthrough into the bugs I encounter. My goal is to explain how I resolved those issues to help other developers build better, faster code

When I watched a lesson on JavaScript Array reduce method and heard the instructor say it's one of the hardest, I simply shrugged - "meh"

That's because at first, I understood its basic process. It reduces the values in the array parameters to a single digit.

Simple right?

Nope!

A few JavaScript exercises later and I'm nearly smacking my head against the wall - "I can't seem to understand this reduce method anymore."

Thanks to web development simplified, I finally understood the method

**So here's a simple function to use reduce: **

function reduceArr((){

})

This can accept 4 parameters. But for purposes of clarity, let's start with the commonly used parameters.

2 parameters alone -

function reduceArr ((total, iteration){

 }, initialElement)

So, this method accepts a callback function and another parameter.

The last parameter serves as the starter "total" parameter

That is, the initialElement parameter is the element that will be the initial total in the first iteration.

Which is usually "0"

So, in the next iteration, the next element of the array ends up in the iteration parameter, and it adds to the total.

And it continues the loop until the end of the array.

So to finalize,

function reduceArr((total, iteration) => { 
return total

},0)

To run the function -

reduceArr([1,2,3,4])

The end result should be 10

Watch Web simplified tutorial here

More from this blog

K

Kodervine's log

45 posts