Home >>JavaScript Array Reference >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 | 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. |
Method | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
reduceRight() | Yes | 9.0 | 3.0 | 4 | 10.5 |
<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>
<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>
Click the button to see the Output.
Output: