Skip to content

median ​

Calculates the median of an array of numbers.

Syntax ​

typescript
median(arr: number[]): number

Parameters ​

NameTypeDescription
arrnumber[]Array of numbers.

Return ​

TypeDescription
numberMedian of the values.

Example ​

typescript
median([1, 2, 3, 4, 5]); // 3
median([1, 2, 3, 4]); // 2.5

Notes ​

  • Throws TypeError if the argument is not an array of numbers.
  • Returns NaN if the array is empty.

References ​

Released under the MIT License.