alt text

Lorem Ipsum

Lorem ipsum dolor sit amet, consectetur adipiscing elit cura bitur ac faucibus.

alt text

Lorem Ipsum

Lorem ipsum dolor sit amet, consectetur adipiscing elit cura bitur ac faucibus.

alt text

Lorem Ipsum

Lorem ipsum dolor sit amet, consectetur adipiscing elit cura bitur ac faucibus.

What is the Difference Between JavaScript Array.reduce() and Array.reduceRight() in JavaScript

In JavaScript,
the difference between Array.prototype.reduce() and Array.prototype.reduceRight() methods is how they traverse elements of an array:

  • Array.prototype.reduce() starts from left-to-right;
  • Array.prototype.reduceRight() starts from right-to-left.

Here is an example, which illustrates the difference between the two:

const arr = ['a', 'b', 'c', 'd'];

const ltr = arr.reduce((previous, current) => previous + current);
const rtl = arr.reduceRight((previous, current) => previous + current);

console.log(ltr); // 'abcd'
console.log(rtl); // 'dcba'
One Comment