Returns the greatest element of elements by comparator (natural order when omitted), the first one found among equals, or undefined when there is none. A single element is returned without a comparison.
elements
comparator
undefined
Time: O(n), exactly n − 1 comparator calls.
Any iterable.
Optional
The order; natural order when omitted.
The greatest element, or undefined.
TypeError when elements is not iterable, comparator is not a function, or it returns a non-number or NaN.
NaN
NullElementError when natural order meets null or undefined.
null
ClassCastError when natural order meets values of different kinds.
max(['pear', 'apple'], (a, b) => a.length - b.length) // 'apple' Copy
max(['pear', 'apple'], (a, b) => a.length - b.length) // 'apple'
Returns the greatest element of
elementsbycomparator(natural order when omitted), the first one found among equals, orundefinedwhen there is none. A single element is returned without a comparison.Time: O(n), exactly n − 1 comparator calls.