Skip to content

mean

Calculates the arithmetic mean (average) of an array of numbers. Returns NaN for an empty array.

Pairs with sum for aggregate operations.

Available since v2.0.0

import { mean } from '@helpers4/array';
mean(array: readonly number[]): number
ParameterTypeDescription
arrayreadonly number[]The array of numbers to average

number — The arithmetic mean, or NaN if the array is empty

Returns the arithmetic mean of the array; NaN for empty arrays.

mean([1, 2, 3, 4])  // => 2.5
mean([10, 20, 30])  // => 20
mean([])            // => NaN

View source on GitHub