Ramda.js R.maxBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Relation

Definition

Ramda’s R.maxBy() answers a projection selection question: which of two values wins when you compare them through a function f? You pass a unary projection, then two values, and get back one of the originals — whichever produces the larger f result. It is not a Boolean test like R.eqBy. Cousin R.max compares values directly; R.minBy picks the smaller by projection. Previous helper is R.max. Next is R.min.

The official sample is the whole idea. const square = n => n * n; then R.maxBy(square, -3, 2) returns -3 because square(-3) is 9 while square(2) is 4. Raw numeric order would pick 2, but projection order picks -3. The result is always one of the two inputs, never f(a) or f(b).

Syntax

Call R.maxBy. Start with this line.

Ramda
R.maxBy(f, a, b)
CallWhat it does
R.maxBy(f, a, b)Primary signature.
R.maxBy(...)Call the relation helper.

Prefer import { maxBy } from "ramda" when you want a named import.

Library declarations
Ramda
R.maxBy(f, a, b)

Parameters

ParameterDescription
fRequired. Function. Unary projection applied to both a and b. Its results are compared for ordering.
aRequired. Any. First value. One of a or b is returned. Not mutated.
bRequired. Any. Second value to compare against. Not mutated.

Return Value

Return ValueDescription
Same type as inputsThe original a or b whose projection f(...) is larger. Never f(a), never a Boolean.

Ramda Examples

📚 Getting Started

The official Ramda square sample and how projection order differs from direct comparison.

Example 1 — Official square Sample

From the Ramda docs. R.maxBy compares projections and returns the original value, not f(result).

Ramda
const square = n => n * n;

R.maxBy(square, -3, 2);
//=> -3
Try It Yourself

How It Works

That is the docs sample from ramdajs.com. square(-3) is 9 and square(2) is 4, so -3 wins. The result is -3, not 9.

Example 2 — vs R.max on the Same Pair

On (-3, 2), direct order picks 2. Projection order via square picks -3.

Ramda
const square = n => n * n;

R.max(-3, 2);
//=> 2

R.maxBy(square, -3, 2);
//=> -3
Try It Yourself

How It Works

R.max compares -3 and 2 directly. R.maxBy(square) compares 9 vs 4 and returns the original -3. Same pair, different question.

📈 Practical Patterns

Record selection by field, reduce maximum-by-key, then curried partial application.

Example 3 — R.maxBy(R.prop('score'), userA, userB)

Pick the user record whose score field is higher. You get back the whole object, not just the score.

Ramda
const userA = { name: 'Ada', score: 92 };
const userB = { name: 'Grace', score: 88 };

R.maxBy(R.prop('score'), userA, userB);
//=> { name: 'Ada', score: 92 }
Try It Yourself

How It Works

R.prop('score') extracts each score. 92 > 88, so the full userA record is returned. This is the idiomatic pattern for picking the winning record by one field.

Example 4 — Maximum by Projection with R.reduce

Official list sample. Fold to the element with the largest square. Empty list returns the seed.

Ramda
const square = n => n * n;

R.reduce(R.maxBy(square), 0, [3, -5, 4, 1, -2]);
//=> -5

R.reduce(R.maxBy(square), 0, []);
//=> 0
Try It Yourself

How It Works

R.reduce walks the list, keeping the element whose square is largest. square(-5) is 25, beating every other element. On an empty list the seed 0 is returned unchanged.

Example 5 — Curried Partial Application with R.__

Fix the projection and one record. The placeholder waits for the other value in a pipeline.

Ramda
const userB = { name: 'Grace', score: 88 };
const pickOverB = R.maxBy(R.prop('score'), R.__, userB);

pickOverB({ name: 'Ada', score: 92 });
//=> { name: 'Ada', score: 92 }

pickOverB({ name: 'Alan', score: 70 });
//=> { name: 'Grace', score: 88 }
Try It Yourself

How It Works

R.maxBy(R.prop('score'), R.__, userB) fixes the projection and the baseline record. Each call compares a new candidate against userB and returns whichever record has the higher score.

Browser Support

R.maxBy() is ordinary JavaScript once Ramda is loaded. Logos use the shared browser-image-sprite.png sprite from this project.

✓ Ramda 0.8.0+ · Any JS runtime

Ramda.js R.maxBy()

Load Ramda 0.8.0 or later via CDN or npm, then call R.maxBy with a projection function and two values. No extra browser API is required.

100% With Ramda 0.8.0+
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
R.maxBy() Universal

Bottom line: Safe in any project that includes Ramda 0.8.0 or later. Use a CDN build such as ramda.min.js in the browser or install ramda from npm for Node. These labs use [email protected].

Frequently Asked Questions

R.maxBy(f, a, b) returns whichever of a or b produces the larger result when passed to f. Type: Ord b => (a → b) → a → a → a. Arity 3, curried. Added in v0.8.0.
const square = n => n * n; R.maxBy(square, -3, 2) returns -3 because square(-3) is 9 and square(2) is 4. The result is the original value, not the projection.
R.max compares a and b directly. R.maxBy compares f(a) vs f(b) and returns the original a or b whose projection is larger. On (-3, 2), R.max returns 2 while R.maxBy(square) returns -3.
R.minBy(fn, a, b) picks the smaller by projection. R.eqBy(f, x, y) returns a Boolean when f(x) equals f(y). R.maxBy picks the larger original value by ordering on f.
Always the original a or b — never f(a) or f(b). You get back the winning input ready to use in pipelines.
No. R.maxBy is a stable Ramda relation helper from v0.8.0. It is not deprecated, experimental, or non-standard.

Continue to R.min()

Pick the smaller of two ordered values — numbers, strings, Dates, and the R.reduce minimum pattern.

R.min() Method →

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.

6 people found this page helpful