Skip to main content

max()

max(array): number | undefined

Computes the maximum value of array.

DEPRECATED

Use Math.max(...array) directly instead.


Parametersโ€‹

array: readonly number[]โ€‹

The array to iterate over.


Returns: number | undefinedโ€‹

The maximum value, or undefined if array is empty.


See Alsoโ€‹

Math.max() - MDN


Sinceโ€‹

2.0.0


Also known asโ€‹

max (Lodash, es-toolkit, Radashi, Ramda) ยท โŒ (Remeda, Effect, Modern Dash, Antfu)


Exampleโ€‹

// โŒ Deprecated approach
max([4, 2, 8, 6]); // => 8
max([]); // => undefined

// โœ… Recommended approach
Math.max(...[4, 2, 8, 6]); // => 8
Math.max(...[]); // => -Infinity (handle empty case manually)

How it works?โ€‹

Computes the maximum value of array. Deprecated: Use Math.max(...array) directly.

Native Equivalentโ€‹

// โŒ max(arr)
// โœ… Math.max(...arr)

Use Casesโ€‹

Find maximum ๐Ÿ“Œโ€‹

Find largest value.

Math.max(1, 5, 3);     // 5
Math.max(...numbers); // from array

Clamp minimumโ€‹

Ensure value is at least minimum.

const atLeast = Math.max(value, minimum);