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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

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

Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>                    

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.