Skip to main content

Function: max()

max<ElementType>(iterable: IterableResolvable<ElementType>): ElementType | null

Consumes the iterable and returns the highest number element. If the iterable is empty, it returns null.

This function uses the default comparator (lexicographically), which means it will compare the elements as strings. If this is undesirable, use maxBy instead.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>An iterator of number values to determine the maximum value of.

Returns

ElementType | null

The maximum value in the input iterator, or null if the iterator is empty or contains only non-number values.

Seealso

maxBy for a version that allows custom comparators.

Seealso

maxByKey for a version that allows custom key extractors.

Example

import { max } from '@sapphire/iterator-utilities';

const iterable = [1, 2, 3, 4, 5];
console.log(max(iterable));
// Output: 5

Remarks

This function consumes the entire iterator.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/max.ts:31