In this section, we will learn what the reduceRight() method is and how to use it.
JavaScript Arrays reduceRight() Method
The `reduceRight()` method runs a reduction operation on each element of an array starting from the rightmost element and moves towards the left-most element.
For example, you might have an array filled with decimal numbers and now want to know what the total sum is if you add up the elements!
This is an example where we can use the reduceRight() method.
Note: there’s another method called `reduce()` that is used pretty much for the same purpose. But the main difference is that it runs the operation from the left-most element of an array and moves towards the last element on the right side of that array. Basically, their starting position is on the opposite side of each other.
Array reduceRight() Syntax:
array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)
Array reduceRight() Method Parameters:
The method takes two arguments:
- The first argument is a reference to a function that will be invoked per each item.
- The second argument is an optional initial value upon which the reduction is based.
The function that we pass to the `reduceRight()` method takes 4 arguments:
- The first argument is the previous value resulted from calling the function.
- The second argument is the current value taken from the target array.
- The third argument is the index number of the current value.
- The fourth argument is a reference to the target array.
Note: on the first iteration (the first call to the function that we pass to the `reduceRight()` method), if the initial value is set in the `reduceRight()` method (as the second argument), it will be used as the first argument to the function. Otherwise, if there’s no initial value, the first argument to the function is the last item in the array and the second argument is the item before the last item in the target array.
Array reduceRight() Method Return Value:
The returned value of this method is the final result of running the reduction operation on every element in an array.
Example: using Array reduceRight() method in JavaScript
const arr = [1,2,3,4,5]; function eve(total, currentValue,currentIndex ,arr){ console.log(`The total value is: ${total} and the currentValue is: ${currentValue}`); return total + currentValue; } const final = arr.reduceRight(eve,10); console.log(final);
Output:
The total value is: 10 and the currentValue is: 5 The total value is: 15 and the currentValue is: 4 The total value is: 19 and the currentValue is: 3 The total value is: 22 and the currentValue is: 2 The total value is: 24 and the currentValue is: 1 25
Here, the initial value is the number 10 and so in the first call to the function, the first argument (`total`) will be 10.
Array reduceRight() method Example:
const arr = [1,2,3,4,5]; function eve(total, currentValue,currentIndex ,arr){ console.log(`The total value is: ${total} and the currentValue is: ${currentValue}`); return total + currentValue; } const final = arr.reduceRight(eve); console.log(final);
Output:
The total value is: 5 and the currentValue is: 4 The total value is: 9 and the currentValue is: 3 The total value is: 12 and the currentValue is: 2 The total value is: 14 and the currentValue is: 1 15
Here, because there’s no-initial value, the first argument (`total`) is basically the last element in the array and the `currentValue` is the element before the last element in the array.