• JavaScript Video Tutorials

JavaScript - Array .reduceRight() Method



In JavaScript, the Array.reduceRight() method applies a function against an accumulator and each element in the array (from right to left) to reduce it to a single value.

This method does not execute the reducer function for empty array elements. In addition to that, it does not modify the original array.

Note − If the current array is empty or doesn't contain any initialValue, this method will throw a 'TypeError' exception.

Difference between reduce() and reduceRight()

Both the reduce() and reduceRight() methods are almost similar, but there is a slight difference between them. The reduce() method iterates over an array from left to right, while the reduceRight() method iterates over an array from right to left.

Syntax

Following is the syntax of JavaScript Array.reduceRight() method −

reduceRight(callbackFn(accumulator, currentValue, currentIndex, array), initialValue)

Parameters

  • callbackFn − This is the function to execute on each element in the array. This function takes four arguments:
    • accumulator − This is the initialValue, or the previously returned value of the function.
    • currentValue − This is the current element being processed in the array. If the initialValue is specified, its value will be the last element, if not its value will be the second to last element.
    • currentIndex (optional) − This is the index of the current element being processed in the array.
    • array (optional) − This is the array on which the reduce() method is called.
  • initialValue (optional) − The value to which the accumulator parameter is initialized when the first time the callback function is called.

Return value

This method returns the single value that is the result after reducing the array.

Examples

Example 1

In the following example, reduceRight() adds up the array elements from the end to the beginning, starting from the last element and using the initial value for the accumulator.

<html>
<body>
   <script>
      const numbers = [10, 20, 30, 40, 50];
      const sum = numbers.reduceRight((accumulator, currentValue) => accumulator + currentValue, 0);
      document.write(sum);
   </script>
</body>
</html>

Output

150

Example 2

In this example, the reduceRight() method starts with the last element of the array (50) and an empty array as the accumulator. It iterates from right to left, pushing each element to the accumulator.

<html>
<body>
   <script>
      const numbers = [10, 20, 30, 40, 50];
      const reversedNumbers = numbers.reduceRight((accumulator, currentValue) => {
         accumulator.push(currentValue);
         return accumulator;
      }, []);
      document.write(reversedNumbers);
   </script>
</body>
</html>

Output

50,40,30,20,10

Example 3

In here, the reduceRight() method starts with the last nested array ([5, 6]) and an empty array as the accumulator. It iterates from right to left, concatenating each array to the accumulator.

<html>
<body>
   <script>
      const arrays = [[1, 2], [3, 4], [5, 6]];
      const flattenedArray = arrays.reduceRight((accumulator, currentValue) => accumulator.concat(currentValue), []);
      document.write(flattenedArray);
   </script>
</body>
</html>

Output

5,6,3,4,1,2

Example 4

If the current array does not contain any element(no initial value available), the reduce() method will throw a "TypeError" exception.

<html>
<body>
   <script>
      const numbers = [];
      try {
         numbers.reduce((accumulator, currentValue) => accumulator * currentValue);
      } catch (error) {
         document.write(error);
      }
   </script>
</body>
</html>

Output

TypeError: Reduce of empty array with no initial value
Advertisements