reduceRight() Function is almost same as reduce() and is also used to reduce the elements of the array to a singe value(Say by adding all the values) but it starts from right to left.
Let us say we have the below array with four elements.
var x = [5, 8, 2, 9]
And want to add all the numbers 5+8+2+9 = 24.
And reduceRight() function helps us achieve the above.
Let us see in the below example.
<html> <body> <script> function myFunction(tot, num){ return tot+num } var x = [5, 8, 2, 9] var y = x.reduceRight(myFunction) document.write(y) </script> </body> </html>
So, in the above code we have array that has 4 numbers.
var x = [5, 8, 2, 9]
And we have declared a function named myFunction() that adds all the values of the array and returns thet result.
function myFunction(tot, num){ return tot+num }
There are two arguments, tot and num. You can consider tot keeps track of the total and num value of the current element.
Next, when the reduceRight() Function is called.
var y = x.reduceRight(myFunction)
It executes 4 times (i.e. Length of array times) and calls myFunction() starting from the right most element i.e. 9, adding all the elements returning the final result as 24.