Name

Array.reduceRight() — reduce an array from right-to-left

Availability

ECMAScript 5

Synopsis

array.reduceRight(f)
array.reduceRight(f, initial)

Arguments

f

A function that combines two values (such as two array elements) and returns a new “reduced” value.

initial

An optional initial value to seed the array reduction with. If this argument is specified, reduceRight() behaves as if it had been inserted at the end of array.

Returns

The reduced value of the array, which is the return value of the last invocation of f.

Description

reduceRight() works like the reduce() method: it invokes the function f n-1 times to reduce the n elements of array to a single value. reduceRight() differs from reduce() only in that it enumerates array elements from right to left (from highest index to lowest) rather than left to right. See Array.reduce() for details.

Example

[2, 10, 60].reduceRight(function(x,y) { return x/y })  // => 3: (60/10)/2

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.