DEV Community

Cover image for How to use Reduce in javascript?
Valentin Antonio
Valentin Antonio

Posted on

3 1

How to use Reduce in javascript?

The reduce() method is used to apply a function to each element in the array to reduce the array to a single value.

Example:


const ages = [33,12,20,16,5,54,21,44,23,55,45,90];

let ageSum = 0;

for(let i=0; i < ages.length; i++){
     ageSum += ages[i];
  }
//result
console.log(ageSum);
// 418

The method:

const ageSum = ages.reduce((total,age) => total + age ,0);
//result
console.log(ageSum);
// 418

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️