How to use array reduceRight in JavaScript
In this article I will go to explain use of reduceRight() in JavaScript.
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:
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:
Ask Your Question
Got a programming related question? You may want to post your question here