Ron Jean-Francois

An illustration of an inflatable pink flamingo in a room with a pool

How to use the Reduce Method in JavaScript


Reduce all elements of an array to a single value

In JavaScript, the .reduce() method allows you to “reduce” an array to a single value. You provide a “reducer” callback function that executes on every element in an array in order, where the return value from the results of the calculations on the previous element are passed in on the next run.

const points = [54, 46, 30, 35, 42]

const total = points.reduce((x,y) => x + y)

console.log(total)

/// 207

Practice using the reduce method and let me know how it goes! You can find me on: Twitter, LinkedIn, or Github

#guide, #javascript, #array, #reduce, #method, #tip

<- Back to Ron's Blog