Ramda.js R.median() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Math

Definition

Ramda’s R.median() takes a list of numbers and returns the median. The official docs samples are R.median([2, 9, 7]) → 7, R.median([7, 2, 10, 9]) → 8, and R.median([]) → NaN. Added in v0.14.0. See also R.mean.

Ramda copies the list, sorts the copy, then picks the middle. Odd length: one center item. Even length: the mean of the two center items. The original array is not mutated.

Syntax

Call R.median. Start with this line.

Ramda
R.median(list)
CallWhat it does
R.median(list)Primary signature.
R.median(...)Call the math helper.

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

Library declarations
Ramda
R.median(list)

Parameters

ParameterDescription
listRequired. an array of numbers. Ramda copies it, sorts the copy, then takes the middle.

Return Value

Return ValueDescription
*Returns a Number: the median.

Ramda Examples

📚 Getting Started

The official odd, even, and empty samples, then how sorting picks the middle.

Example 1 — The Official Docs Example

From ramdajs.com/docs/#median: odd list 7, even list 8, empty list NaN.

Ramda
R.median([2, 9, 7]);      //=> 7
R.median([7, 2, 10, 9]);  //=> 8
R.median([]);             //=> NaN
R.median([10]);           //=> 10
Try It Yourself

How It Works

Three items sort to [2, 7, 9]; the middle is 7. Four items sort to [2, 7, 9, 10]; the two middles average to 8. An empty list has no middle, so the result is NaN.

Example 2 — Odd vs Even After Sorting

Order in the input does not matter. Median always sorts a copy first.

Ramda
var odd = [2, 9, 7];
var even = [7, 2, 10, 9];

R.median(odd);    //=> 7   (sorted [2, 7, 9])
R.median(even);   //=> 8   (sorted [2, 7, 9, 10]; (7+9)/2)
odd;              //=> [2, 9, 7]  (unchanged)
Try It Yourself

How It Works

Ramda slices a copy, then sorts that copy. Your odd array stays [2, 9, 7]. Even lists reuse R.mean on the two central items — that is why 7 and 9 become 8.

📈 Practical Patterns

Outliers vs mean, object fields, and guarding empty lists.

Example 3 — R.median vs R.mean

A single large value pulls the mean. The median stays on the middle item.

Ramda
R.median([1, 2, 100]);  //=> 2
R.mean([1, 2, 100]);    //=> 34.333333333333336
Try It Yourself

How It Works

Sorted [1, 2, 100] has middle 2. The mean is (1 + 2 + 100) / 3, about 34.33. The docs pair these helpers so you can pick the typical value or the arithmetic average.

Example 4 — Median of an Object Field

Pluck a numeric property, then take the median of the resulting list.

Ramda
var students = [
  { name: "Ada", score: 80 },
  { name: "Grace", score: 90 },
  { name: "Alan", score: 70 }
];

R.pipe(R.pluck("score"), R.median)(students);
//=> 80
Try It Yourself

How It Works

R.pluck("score") yields [80, 90, 70]. Sorted that is [70, 80, 90], so the median is 80.

Example 5 — Guard Empty Lists with R.defaultTo

R.defaultTo treats NaN as missing, so you can replace the official empty-list result with a fallback.

Ramda
R.median([]);                          //=> NaN
R.defaultTo(0, R.median([]));          //=> 0
R.defaultTo(0, R.median([2, 9, 7]));   //=> 7
Try It Yourself

How It Works

R.defaultTo(d, v) returns d when v is null, undefined, or NaN. That turns the official empty-list NaN into a safe zero (or whatever default you pick).

Browser Support

R.median() is plain JavaScript sorting and arithmetic — it runs anywhere Ramda loads: every modern browser plus Node.js. Logos use the shared browser-image-sprite.png sprite from this project.

✓ Ramda 0.14.0+ · Any JS runtime

Ramda.js R.median()

Load Ramda via CDN or npm, then call R.median(list). Because it only copies, sorts, and averages two middles when needed, behavior is identical across engines.

100% With Ramda loaded
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.median() Universal

Bottom line: Safe in any project that includes Ramda. Use a CDN build such as ramda.min.js in the browser or install ramda from npm for Node.

Frequently Asked Questions

R.median(list) returns the median of a list of numbers: the middle value after sorting. For an odd-length list that is one item. For an even-length list it is the average of the two middle items.
R.median([2, 9, 7]) returns 7. R.median([7, 2, 10, 9]) returns 8. R.median([]) returns NaN. Those are the samples on ramdajs.com/docs/#median. Added in v0.14.0. See also R.mean.
Yes. It has arity 1 and is automatically curried. Call R.median([2, 9, 7]) for a number. Calling R.median() with no argument returns a function that waits for the list.
R.median is the middle sorted value (or the average of the two middles). R.mean is the arithmetic average (sum / length). Outliers pull the mean: R.mean([1, 2, 100]) is about 34.33, while R.median([1, 2, 100]) is 2.
No. It copies the list, then sorts the copy. Your original array stays in the same order.
No. R.median is a stable Ramda math helper from v0.14.0. It is not deprecated, experimental, or non-standard.

Review R.mean()

Compare the arithmetic average with the middle-value helper you just learned.

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

6 people found this page helpful