JavaScript Array reduceRight()
Examples
Subtract the numbers in the array from the last number:
// Reducer Function
function myFunc(total, num) {
return total - num;
}
// Create a Typed Array
const myArr = Int32Array.of(40, 100, 1, 5, 25, 10);
// Reduce the Array to a Number
let number = myArr.reduceRight(myFunc);
Try it Yourself »
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
typed-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. |
JavaScript Typed Arrays
Browser Support
typed-array.reduceRight()
is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is fully supported in all modern browsers since June 2017:
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |