instance method Enumerable#max

View source on GitHub →

Enumerable#max([iterator = Prototype.K[, context]]) → maxValue
  • iterator (Function) – An optional function to use to evaluate each element in the enumeration; the function should return the value to test. If this is not provided, the element itself is tested.
  • context (Object) – An optional object to use as this within calls to the iterator.

Returns the maximum element (or element-based iterator result), or undefined if the enumeration is empty. Elements are either compared directly, or by first calling iterator and comparing returned values. If multiple "max" elements (or results) are equivalent, the one closest to the end of the enumeration is returned.

If provided, iterator is called with two arguments: The element being evaluated, and its index in the enumeration; it should return the value max should consider (and potentially return).

Examples
['c', 'b', 'a'].max();
// -> 'c'
 [1, 3, '3', 2].max();
// -> '3' (because both 3 and '3' are "max", and '3' was later)
 ['zero', 'one', 'two'].max(function(item) { return item.length; });
// -> 4