How to use array reduceRight in JavaScript

In this article I will go to explain use of reduceRight() in JavaScript.
  • 2111

Array reduceright() in JavaScript

The JavaScript array reduceRight() iteratively reduce an array to a single value using a callback function from right to left.

Syntax of reduceright method

array.reduceRight(callback[, initalvalue]);

 

Parameters in reduce method:

  • callback

    Callback function to execute for each item.
     

  • initialvalue

    Initial value object is use as the first argument to the first call of the call back.

Return value in reduceRight  method

It returns the reduce righted single value of the array.

 

Exception in reduceRight  method

If callback argument is not a function object and array contain no element and initial value is not provide, A TypeError exception is fire.
 

Example of reduceRight method
 

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Array ReduceRight Method</title>
</head>
<body>
<script type="text/javascript">
function ReduceRightMeth(previousValue, currentValue) {
    return previousValue + currentValue;
}
var word = "dohteMecudeR";
var result = [].reduceRight.call(word, ReduceRightMeth, "The ");
// var result = Array.prototype.reduceRight.call(word, ReduceRightMeth, "the ");
document.write(result);
</script>
</body>


Output:

 

The ReduceMethod


Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.