What is the Difference Between JavaScript Array.reduce() and Array.reduceRight() in JavaScript
Posted On November 6, 2022
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
I am really grateful to the owner of this web site who has shared this
great post at at this place.