max
Returns the maximum value in an array using a loop instead of spread,
avoiding the call stack overflow that occurs with Math.max(...array)
for very large arrays (> ~65 000 elements).
null and undefined are treated as empty arrays and return undefined.
Available since v2.0.2
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
array | readonly number[] | null | undefined | Array of numbers |
Returns
Section titled “Returns”number | undefined — Maximum value, undefined for empty arrays, null, undefined, or NaN if any element is NaN
Examples
Section titled “Examples”Safe maximum for large arrays
Section titled “Safe maximum for large arrays”Unlike Math.max(…array), max() uses a loop and handles arrays of any size without stack overflow.
