median
Calculates the median (middle value) of an array of numbers. For an even-length array,
returns the average of the two middle values. Returns NaN for an empty array.
Does not mutate the input array.
The median is the 50th percentile — this delegates to it rather than duplicating its sort/interpolation logic.
Available since v3.0.3
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
array | readonly number[] | The array of numbers |
Returns
Section titled “Returns”number — The median value, or NaN if the array is empty
Examples
Section titled “Examples”Odd-length array
Section titled “Odd-length array”Returns the middle value once sorted.
Even-length array
Section titled “Even-length array”Averages the two middle values.
