JavaScript Array reduceRight()
Examples
Subtract the numbers in the array, starting from the end:
const numbers = [175, 50, 25];
document.getElementById("demo").innerHTML
= numbers.reduceRight(myFunc);
function myFunc(total, num) {
return total - num;
}
Try it Yourself »
Subtract the numbers, right-to-left, and display the sum:
const numbers = [2, 45, 30, 100];
document.getElementById("demo").innerHTML = numbers.reduceRight(getSum);
function getSum(total, num) {
return total - num;
}
Try it Yourself »
Definition and Usage
The reduceRight()
method executes a reducer function for each array element.
The reduceRight()
method works from right to left.
The reduceRight()
method returns a single value: the function's accumulated result.
The reduceRight()
method does not execute the function for empty elements.
See Also:
Syntax
array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)
Parameters
Parameter | Description | ||||||||
function() | Required. A function to be run for each element in the array. |
||||||||
Reducer function parameters:
| |||||||||
initialValue | Optional. A value to be passed to the function as the initial value |
Return Value
The accumulated result from the last call of the callback function. |
Browser Support
reduceRight()
is an ECMAScript5 (ES5) feature.
ES5 (JavaScript 2009) fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |