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