Example 1 — Official Average
Average four numbers from the docs.
_.mean([4, 2, 8, 6]);
// → 5 How It Works
- Sum.
4 + 2 + 8 + 6 = 20. - Length. Four elements.
- Divide.
20 / 4 = 5. - Result. Arithmetic mean of the array.

Computes the arithmetic mean of the values in array (sum ÷ length). Part of Lodash 4.x Math (since 4.0.0).
Pass the numbers or collection shown below. Check the return value before you chain another math helper.
Call _.mean. Start with this line.
_.mean(array) | Call | What it does |
|---|---|
_.mean(array) | Primary signature. |
Prefer import mean from "lodash/mean" when you want a smaller bundle.
_.mean(array) | Parameter | Description |
|---|---|
| array | Required. Array of values to average |
| Return Value | Description |
|---|---|
| * | Mean number, or NaN if empty/nullish |
Average four numbers from the docs.
_.mean([4, 2, 8, 6]);
// → 5 4 + 2 + 8 + 6 = 20.20 / 4 = 5.No values means no defined average.
_.mean([]);
// → NaN
_.mean(null);
// → NaN
Number.isFinite(_.mean([]));
// → false 0, length is 0.0 / 0 → NaN.null/undefined input.Number.isFinite before display.Missing values affect the divisor.
_.mean([10, undefined, 20]);
// → 10 // (10 + 20) / 3
_.mean([10, null, 20]);
// → 10 // (10 + 0 + 20) / 3
_.mean([10, 20]);
// → 15 0 in the sum.One string can break the average via concatenation.
_.mean([10, "20", 30]);
// → 34010 // concat during sum, then ÷ 3
_.mean([10, 20, 30].map(Number));
// → 20
_.mean([10, _.toNumber("20"), 30]);
// → 20 10 + "20" becomes "1020", then more joining.Number or _.toNumber._.sum and _.meanByPick the helper that matches the job.
var nums = [4, 2, 8, 6];
var rows = [{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }];
_.mean(nums);
// → 5
_.sum(nums);
// → 20
_.meanBy(rows, "n");
// → 5 _.mean is a Lodash 4.x Math helper (since 4.0.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native cousin: sum with reduce then divide by length.
Available in [email protected] full builds and the lodash/mean modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.
Bottom line: Safe for production once Lodash is loaded. Prefer import mean from "lodash/mean" for smaller builds. Use for simple numeric averages; use meanBy for object fields.
Average object fields with an iteratee or property path.
CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
9 people found this page helpful