Home >>JavaScript Array Reference >JavaScript Array reduceRight() Method

JavaScript Array reduceRight() Method

JavaScript Array reduceRight() Method

JavaScript reduceRight() method is used to reduces the given input array to a single value. Its return value is stored in an accumulator.It accepts two parameters the previous and current elements of the given input array.

Syntax:
array.reduceRight(previousValue, currentValue);

Parameter Values

Parameter Description
previousValue This is a required parameter. It defines a function to be run for each element in the array.
currentValue This is an optional parameter. It defines a value to be passed to the function as the initial value.

Browser Support

Method Chrome Edge Firefox Safari Opera
reduceRight() Yes 9.0 3.0 4 10.5
Here is an example of JavaScript reduceRight() method:
<html>
<body>
<p id="demo"></p>
<script>var numbers = [100, 20, 10];
document.getElementById("demo").innerHTML = numbers.reduceRight(myFunc);
function myFunc(total, num) 
{
  return total - num;
}
</script>
</body>
</html>
Output:

Example 2:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<p>Output: <span id="demo1"></span></p>
<script>
var numbers = [5, 25, 50, 100];
function getSum(total, num) 
{
  return total - num;
}
function myFunction1(item) 
{
  document.getElementById("demo1").innerHTML = numbers.reduceRight(getSum);
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

Output:


No Sidebar ads