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)
Call
What 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
Parameter
Description
min
Required. Ord. Inclusive lower bound. Must not be greater than max.
max
Required. Ord. Inclusive upper bound. Must not be less than min.
value
Required. Ord. The value to restrict. Compared with < and >. Not mutated. Not Number() coerced.
Return Value
Return Value
Description
Same type as the inputs
min 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.
1
10
4
min must not be greater than max in clamp(min, max, value)
How It Works
Math.min(max, Math.max(min, n)) is the number twin for ordered bounds. Lodash _.clampNumber() 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"
m
a
2024-01-01T00:00:00.000Z
2024-06-15T00:00:00.000Z
2024-12-31T00:00:00.000Z
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.
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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll 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.