JavaScript Array reduceRight()
Examples
Subtract the numbers in the array, starting from the end:
document.getElementById("demo").innerHTML = numbers.reduceRight(myFunc);
function myFunc(total, num) {
return total - num;
}
Subtract the numbers, right-to-left, and display the sum:
document.getElementById("demo").innerHTML = numbers.reduceRight(getSum);
function getSum(total, num) {
return total - num;
}
Description
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.
Note
At the first callback, there is no return value from the previous callback.
Normally, the last array element is used as initial value, and the iteration starts from the element before.
If an initial value is supplied, this is used, and the iteration starts from last element.
See Also:
Syntax
Parameters
A function to be run for each element in the array.
The initialValue, or the previously returned value of the function.
The value of the current element.
The index of the current element.
The array the element belongs to.
A value to be passed to the function as the initial value
Return Value
Array Tutorials:
Browser Support
reduceRight()
is an ECMAScript5 (ES5 2009) feature.
JavaScript 2009 is supported in all browsers since July 2013:
Chrome 23 |
IE/Edge 11 |
Firefox 21 |
Safari 6 |
Opera 15 |
Sep 2012 | Sep 2012 | Apr 2013 | Jul 2012 | Jul 2013 |