Describing the reduce Operation
- The
reduceoperation is used for testing if every element of an array satisfies a condition -
It accepts a callback function with the following arguments:
accumulator: Accumulated value previously returnedcurrentValue:Current element processed in the arrayindex: Optional index ofcurrentValuearray: Optional array that is calledthisArg: Optional value to use asthis
Defining an Array
let fruits = ["Apple", "Banana"];
console.log(fruits.length);
// 2Using the reduce Operation
// Regular function
fruits.reduce(function(accum, item) {
return accum + item;
})
// "AppleBanana"
// Arrow function
fruits.reduce((accum, item) => accum + item);
// "AppleBanana"Previous