For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/math/mean.md.

mean es-toolkit

Calculates the average of an array of numbers.

const average = mean(nums);

Usage

mean(nums)

Use mean when you want to find the average value of an array of numbers. It calculates the average by adding all numbers together and dividing by the length of the array. If given an empty array, it returns NaN.

import { mean } from 'es-toolkit/math';

// Calculate the average of an array of numbers
const numbers = [1, 2, 3, 4, 5];
const result = mean(numbers);
// result is 3 ((1 + 2 + 3 + 4 + 5) / 5 = 15 / 5 = 3)

// Calculate the average of decimal numbers
const decimals = [1.5, 2.5, 3.5];
const decimalResult = mean(decimals);
// decimalResult is 2.5

// Returns NaN for an empty array
const emptyResult = mean([]);
// emptyResult is NaN

Parameters

numsreadonly number[]required

An array of numbers to calculate the average.

Returns

Returns the average of all numbers in the array. Returns NaN if the array is empty.

number
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.