Lodash _.mean() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Math

Definition

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.

Syntax

Call _.mean. Start with this line.

Lodash
_.mean(array)
CallWhat it does
_.mean(array)Primary signature.

Prefer import mean from "lodash/mean" when you want a smaller bundle.

Library declarations
Lodash
_.mean(array)

Parameters

ParameterDescription
arrayRequired. Array of values to average

Return Value

Return ValueDescription
*Mean number, or NaN if empty/nullish

Lodash Examples

Example 1 — Official Average

Average four numbers from the docs.

Lodash
_.mean([4, 2, 8, 6]);
// → 5
Try It Yourself

How It Works

  1. Sum. 4 + 2 + 8 + 6 = 20.
  2. Length. Four elements.
  3. Divide. 20 / 4 = 5.
  4. Result. Arithmetic mean of the array.

Example 2 — Empty Array Returns NaN

No values means no defined average.

Lodash
_.mean([]);
// → NaN

_.mean(null);
// → NaN

Number.isFinite(_.mean([]));
// → false
Try It Yourself

How It Works

  1. Empty. Sum is 0, length is 0.
  2. Divide. 0 / 0 → NaN.
  3. Nullish. Same fallback for null/undefined input.
  4. Guard. Check with Number.isFinite before display.

Example 3 — undefined and null Holes

Missing values affect the divisor.

Lodash
_.mean([10, undefined, 20]);
// → 10          // (10 + 20) / 3

_.mean([10, null, 20]);
// → 10          // (10 + 0 + 20) / 3

_.mean([10, 20]);
// → 15
Try It Yourself

How It Works

  1. undefined. Skipped in the sum, still counted in length.
  2. null. Coerces to 0 in the sum.
  3. Clean list. Only real numbers → expected mid-point.
  4. Fix. Filter holes first if you want “known values only.”

Example 4 — String Pitfall

One string can break the average via concatenation.

Lodash
_.mean([10, "20", 30]);
// → 34010        // concat during sum, then ÷ 3

_.mean([10, 20, 30].map(Number));
// → 20

_.mean([10, _.toNumber("20"), 30]);
// → 20
Try It Yourself

How It Works

  1. Concat. 10 + "20" becomes "1020", then more joining.
  2. Divide. That huge string coerces to a number ÷ length.
  3. Coerce. Map with Number or _.toNumber.
  4. Rule. Keep arrays numeric before averaging.

Example 5 — vs _.sum and _.meanBy

Pick the helper that matches the job.

Lodash
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
Try It Yourself

How It Works

  1. mean. Average of raw numbers.
  2. sum. Total without dividing.
  3. meanBy. Average a field on objects.
  4. Choose. Numbers → mean; objects → meanBy; total → sum.

Browser & Runtime Support

_.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.

✓ Stable · Lodash 4.0.0+

_.mean()

Available in [email protected] full builds and the lodash/mean modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.mean Lodash 4.0+

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.

Frequently Asked Questions

It computes the arithmetic mean of the values in array. Official: _.mean([4, 2, 8, 6]) → 5.
NaN — not 0 or undefined. Empty or nullish input yields NaN (effectively 0 / 0).
mean averages the elements themselves. meanBy averages a projected value per element (function or property path).
sum returns the total. mean returns total / length.
undefined is skipped in the sum but still counted in length, which pulls the average down. Prefer filtering first when that matters.
Yes. A string switches + to concatenation during the sum, so the average can become nonsense. Coerce with Number / _.toNumber first.

Next: _.meanBy()

Average object fields with an iteratee or property path.

_.meanBy() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful