Tutorialspoint.dev

JavaScript | array.reduceRight()

The array.reduceRight() is an inbuilt function in JavaScript which is used to convert elements of the given array from right to left to a single value.
Syntax:

array.reduceRight(previousValue, currentValue)

Parameters: It accepts two parameters previousValue and currentValue which represents the previous and current element of the given input array.
Return Values: It returns the result after reduction to a single value.

JavaScript code which shows the working of array.reduceRight() function:

Code #1:

<script>
  
// Taking some array as the element of an array "A"
const A = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8 ] ];
  
// Calling array.reduceRight() function
a = A.reduceRight((previousValue, currentValue) => previousValue.concat(currentValue));
  
// printing result
document.write(a);
  
</script>

Output:

7, 8, 4, 5, 6, 1, 2, 3

Code #2:

<script>
  
// Taking some array as the element of an array "A"
const A = [ [ 1, 2, 3 ], [ "a", "b", "c" ], [ 7, 8 ] ];
  
// Calling array.reduceRight() function
a = A.reduceRight((previousValue, currentValue) => previousValue.concat(currentValue));
  
// printing result
document.write(a);
  
</script>                    

Output:

7, 8, a, b, c, 1, 2, 3


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter