Lodash _.maxBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Math

Definition

Like _.max, except it accepts an iteratee invoked for each element to generate the ranking criterion. 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 _.maxBy. Start with this line.

Lodash
_.maxBy(array, [iteratee])
CallWhat it does
_.maxBy(array, [iteratee])Primary signature.
_.maxBy(array, iteratee)Run the math helper on numbers or a collection.

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

Library declarations
Lodash
_.maxBy(array, [iteratee])

Parameters

ParameterDescription
arrayRequired. Collection to scan
iterateeOptional. Ranking function or property shorthand; default identity

Return Value

Return ValueDescription
*Winning element, or undefined if empty/falsey

Lodash Examples

Example 1 — Function Iteratee

Rank objects with an explicit callback.

Lodash
var objects = [{ n: 1 }, { n: 2 }];

_.maxBy(objects, function (o) {
  return o.n;
});
// → { n: 2 }
Try It Yourself

How It Works

  1. Score. Each object yields o.n.
  2. Compare. 2 > 1 → second object wins.
  3. Return. The whole { n: 2 } element is returned.
  4. Docs. Matches the first official example.

Example 2 — Property Shorthand

Pass a string property name instead of a function.

Lodash
var objects = [{ n: 1 }, { n: 2 }];

_.maxBy(objects, "n");
// → { n: 2 }
Try It Yourself

How It Works

  1. Shorthand. "n" becomes a property iteratee.
  2. Same ranks. Scores are still 1 and 2.
  3. Same winner. { n: 2 } is returned.
  4. Style. Prefer shorthand for simple fields.

Example 3 — Nested Property Path

Use a dotted path for nested scores.

Lodash
var players = [
  { name: "Ada", score: { value: 10 } },
  { name: "Lin", score: { value: 25 } },
  { name: "Sam", score: { value: 18 } }
];

_.maxBy(players, "score.value");
// → { name: "Lin", score: { value: 25 } }
Try It Yourself

How It Works

  1. Path. "score.value" reads the nested number.
  2. Ranks. 10, 25, 18 → 25 wins.
  3. Element. The full Lin object is returned.
  4. Alt. A function p => p.score.value works the same.

Example 4 — Empty → undefined

Same empty rule as _.max.

Lodash
_.maxBy([], "n");
// → undefined
Try It Yourself

How It Works

  1. Guard. Empty or falsey arrays short-circuit.
  2. No winner. There is no element to return.
  3. Result. undefined.
  4. App tip. Default with nullish coalescing when needed.

Example 5 — maxBy vs minBy

Same iteratee; opposite ranking.

Lodash
var objects = [{ n: 1 }, { n: 2 }, { n: 0 }];

[
  _.maxBy(objects, "n"),
  _.minBy(objects, "n")
];
// → [{ n: 2 }, { n: 0 }]
Try It Yourself

How It Works

  1. maxBy. Picks the largest score → { n: 2 }.
  2. minBy. Picks the smallest score → { n: 0 }.
  3. API. Same iteratee / shorthand style for both.
  4. Plain numbers. Prefer _.max / _.min without an iteratee.

Browser & Runtime Support

_.maxBy is a Lodash 4.x Math helper (since 4.0.0). It needs no special browser APIs beyond loading Lodash.

✓ Stable · Lodash 4.0.0+

_.maxBy()

Available in [email protected] full builds and the lodash/maxBy modular import. Works in evergreen browsers and Node / Bun / Deno 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
_.maxBy Lodash 4.x+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import maxBy from "lodash/maxBy" for smaller builds.

Frequently Asked Questions

_.maxBy(array, [iteratee=_.identity]) returns the element whose iteratee result is greatest. Official docs: _.maxBy([{n:1},{n:2}], 'n') → {n:2}.
The original array element that wins, not the iteratee number. You get the object back, not just 2.
Yes. Lodash supports property shorthand such as 'n' or nested paths like 'score.value'.
_.maxBy([]) returns undefined — same empty/falsey rule as _.max.
_.max compares elements as-is and takes no iteratee. _.maxBy ranks by iteratee results — use it for objects.
Use import maxBy from "lodash/maxBy" or require("lodash/maxBy") for tree-shaking. Full build: import _ from "lodash" then _.maxBy.

Next: _.mean()

Compute the arithmetic average of the values in an array.

_.mean() tutorial →

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.

10 people found this page helpful