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);