reduce and reduceRight in JavaScript

reduce

processes the array from left to right (starting from the first element and ending with the last element).

const arr = ['a', 'b', 'c', 'd'];
const result = arr.reduce((acc, curr) => acc + curr, '');
console.log(result); // Output: "abcd"

reduceRight

processes the array from right to left (starting from the last element and ending with the first element).

const arr = ['a', 'b', 'c', 'd'];
const result = arr.reduceRight((acc, curr) => acc + curr, '');
console.log(result); // Output: "dcba"

Leave a comment

Your email address will not be published. Required fields are marked *