The reduceRight() method runs a function on each array element to produce (reduce it to) a single value.

The reduceRight() works from right-to-left in the array. See also reduce().

The reduceRight() method does not reduce the original array.

This example finds the sum of all numbers in an array:

Example

const numbers = [45, 4, 9, 16, 25];
let sum = numbers1.reduceRight(myFunction);

function myFunction(total, value, index, array) {
  return total + value;
}

Note that the function takes 4 arguments:

  • The total (the initial value / previously returned value)
  • The item value
  • The item index
  • The array itself

The example above does not use the index and array parameters. It can be rewritten to:

Example

const numbers = [45, 4, 9, 16, 25];
let sum = numbers1.reduceRight(myFunction);

function myFunction(total, value) {
  return total + value;
}
Array.reduceRight() is supported in all browsers except Internet Explorer 8 or earlier.



Practice Excercise Practice now