English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript array reduceRight() method

 JavaScript Array Object

reduceRight() The function is the same as the reduce() function, but the reduceRight() function adds the array items from the end of the array to the beginning.

reduceRight()The method executes the callback function once for each array index.

The return value of the function is stored in the accumulator (result).

Syntax:

array.reduceRight(callback, initialValue)
var nums = [[0, 1], [2, 3], [4, 5];
function fire(x, y) {
   return x.concat(y);
}
function myFunc() {
   document.getElementById("result").innerHTML = nums.reduceRight(fire);
}
Test and See‹/›

Browser compatibility

The numbers in the table specify the first browser version that fully supports the reduceRight() method:

Method
reduceRight()is310.549

Parameter value

ParameterDescription
callback
a function that is run for each element in the array.
Function parameters:
  • accumulator(required)- function'sinitialValueor the previously returned value

  • element(required)-the current element being processed in the array

  • index(optional)-the index of the current element being processed in the array

  • array(optional)- invoked an arrayreduceRight()

initialValue(Optional) The value used as the first argument of the callback for the initial call. If an initial value is not provided, the first element of the array will be used.

Technical Details

Return Value:Reduce the number of generated values
JavaScript Version:ECMAScript 5

More Examples

Difference between reduce() and reduceRight():

var arr = ['1''2''3''4''5'];
function funcReduce() {
var val = arr.reduce(function(x, y) { return x + y;});
document.getElementById("result").innerHTML = val;
}
function funcReduceRight() {
var val = arr.reduceRight(function(x, y) { return x + y;});
document.getElementById("result").innerHTML = val;
}
Test and See‹/›

 JavaScript Array Object