Ramda.js R.clamp() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Relation

Definition

Ramda’s R.clamp() answers a range question: keep this value between min and max, inclusive? You pass min, then max, then value. You get back the nearest legal value: min when too small, max when too large, otherwise the original. Native numbers often use Math.min(max, Math.max(min, n)). That twin only holds when min <= max. Ramda is a function, arity 3, automatically curried, so R.clamp(0, 100) is already a function of one value. It also works for other ordered types such as strings and Dates. Unlike Lodash _.clamp, Ramda does not Number() coerce, and it throws if min is greater than max.

The official sample is the whole idea. Three numbers against 1 and 10: too low becomes 1, too high becomes 10, 4 stays 4. Need the smaller of two values only? That is R.min. Need the larger? That is R.max. Previous helper is R.xor. Next is R.countBy.

Syntax

Call R.clamp. Start with this line.

Ramda
R.clamp(min, max, value)
CallWhat it does
R.clamp(min, max, value)Primary signature.
R.clamp(...)Call the relation helper.

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

Library declarations
Ramda
R.clamp(min, max, value)

Parameters

ParameterDescription
minRequired. Ord. Inclusive lower bound. Must not be greater than max.
maxRequired. Ord. Inclusive upper bound. Must not be less than min.
valueRequired. Ord. The value to restrict. Compared with < and >. Not mutated. Not Number() coerced.

Return Value

Return ValueDescription
Same type as the inputsmin when value < min. max when value > max. Otherwise value. Throws if min > max.

Ramda Examples

📚 Getting Started

The official Ramda 1–10 sample, then the same idea compared with Math.min/Math.max and reversed bounds.

Example 1 — Official Number Sample

When the range is 1 to 10, a value below min becomes 1, a value above max becomes 10, and a value inside stays itself.

Ramda
R.clamp(1, 10, -5);
//=> 1

R.clamp(1, 10, 15);
//=> 10

R.clamp(1, 10, 4);
//=> 4
Try It Yourself

How It Works

That is the docs sample from ramdajs.com. Inclusive range: -5 is below 1, 15 is above 10, 4 is already inside.

Example 2 — vs Math.min/Math.max and reversed bounds

The native number twin matches when min is not greater than max. Lodash would apply upper then lower without swapping. Ramda throws.

Ramda
Math.min(10, Math.max(1, -5));
//=> 1

Math.min(10, Math.max(1, 15));
//=> 10

Math.min(10, Math.max(1, 4));
//=> 4

try {
  R.clamp(10, 1, 5);
} catch (err) {
  err.message;
}
//=> "min must not be greater than max in clamp(min, max, value)"
Try It Yourself

How It Works

Math.min(max, Math.max(min, n)) is the number twin for ordered bounds. Lodash _.clamp Number() coerces and still runs when lower is above upper: it applies the upper bound first, then the lower bound, and does not swap them. Ramda refuses that case. Previous helper is R.xor.

📈 Practical Patterns

Ordered types that are not numbers, then a leftover for map, then a percent pipe for volume and progress.

Example 3 — Strings and Dates

Any ordered type works. Characters clamp by string order. Dates clamp by their time values. There is no Number() coerce.

Ramda
R.clamp('a', 'z', 'm');
//=> "m"

R.clamp('a', 'z', '0');
//=> "a"

var minDate = new Date(Date.UTC(2024, 0, 1));
var maxDate = new Date(Date.UTC(2024, 11, 31));
var early = new Date(Date.UTC(2023, 5, 1));
var mid = new Date(Date.UTC(2024, 5, 15));
var late = new Date(Date.UTC(2025, 0, 1));

R.clamp(minDate, maxDate, early).toISOString();
//=> "2024-01-01T00:00:00.000Z"

R.clamp(minDate, maxDate, mid).toISOString();
//=> "2024-06-15T00:00:00.000Z"

R.clamp(minDate, maxDate, late).toISOString();
//=> "2024-12-31T00:00:00.000Z"
Try It Yourself

How It Works

'm' sits between 'a' and 'z'. Digit '0' is before 'a' in string order, so the result is the min. Dates compare by millisecond. Lodash would have tried to turn these into numbers first.

Example 4 — Curried Leftover with R.map

Fill min and max. Map that function over a list of scores or percents.

Ramda
R.clamp(0, 100)(150);
//=> 100

R.map(R.clamp(0, 100), [-10, 40, 150]);
//=> [0, 40, 100]
Try It Yourself

How It Works

Because clamp is arity 3, filling min and max leaves a function of one value for R.map. Nested Math.min/Math.max needs a wrapper lambda every time.

Example 5 — Pipe a Volume or Progress Percent

Coerce then clamp 0–100 so a slider, volume, or progress bar never leaves the percent scale.

Ramda
var clampPercent = R.clamp(0, 100);

function toProgress(raw) {
  return R.pipe(
    function (n) { return Number(n); },
    clampPercent
  )(raw);
}

toProgress(-10);
//=> 0

toProgress(40);
//=> 40

toProgress(150);
//=> 100

toProgress('80');
//=> 80
Try It Yourself

How It Works

Clamp itself does not Number() coerce, so a string leftover would stay a string. The pipe converts first, then bounds. Volume and progress both live on 0–100. Inclusive 0 and 100 stay legal.

Browser Support

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

✓ Ramda 0.20.0+ · Any JS runtime

Ramda.js R.clamp()

Load Ramda 0.20.0 or later via CDN or npm, then call R.clamp with min, max, and a value. No extra browser API is required.

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

Bottom line: Safe in any project that includes Ramda 0.20.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.clamp(min, max, value) restricts value to an inclusive range. If value is below min you get min. If it is above max you get max. Otherwise you get value unchanged. Type: Ord a => a → a → a → a. Arity 3, curried. Added in v0.20.0.
R.clamp(1, 10, -5) is 1. R.clamp(1, 10, 15) is 10. R.clamp(1, 10, 4) is 4.
For numbers with min <= max, Math.min(max, Math.max(min, n)) matches Ramda. Lodash _.clamp Number()-coerces and applies the upper bound first, then the lower bound; it does not swap reversed bounds. Ramda throws if min > max and does not Number() coerce, so strings and Dates stay their own types.
Yes. Any ordered type that JavaScript can compare with < and > works. R.clamp('a', 'z', 'm') is 'm'. R.clamp('a', 'z', '0') is 'a'. Dates clamp by their time values.
Ramda throws Error: min must not be greater than max in clamp(min, max, value). Lodash does not throw; it still applies upper then lower. Native Math.min/Math.max with swapped bounds is a different expression, not an error.
No. R.clamp is a stable Ramda relation helper from v0.20.0. It is not deprecated, experimental, or non-standard.

Continue to R.countBy()

Learn how to count list elements by keys from a function with Ramda R.countBy().

R.countBy() 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