Lodash _.clamp() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Number

Definition

Clamps number within the inclusive lower and upper bounds. Part of Lodash Number (stable since 4.0.0 — not deprecated). Too small → lower; too large → upper; otherwise unchanged.

Pass the numbers shown below. Check bounds and return type before you use the result.

Syntax

Call _.clamp. Start with this line.

Lodash
_.clamp(number, lower, upper)
CallWhat it does
_.clamp(number, lower, upper)Primary signature.
_.clamp(number, [lower], upper)Work with a number and its bounds.

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

Library declarations
Lodash
_.clamp(number, lower, upper)

Parameters

ParameterDescription
numberRequired. Value to clamp
lowerOptional. Inclusive lower bound
upperRequired. Inclusive upper bound

Return Value

Return ValueDescription
*The clamped number

Lodash Examples

Example 1 — Official: Below Lower Bound

Values under the floor snap up to lower.

Lodash
console.log(_.clamp(-10, -5, 5)); // -5
Try It Yourself

How It Works

  1. Value. -10 is below -5.
  2. Floor. Raised to the lower bound.
  3. Result. -5 — matches the official docs.
  4. Tip. Bounds are inclusive.

Example 2 — Official: Above Upper Bound

Values over the ceiling snap down to upper.

Lodash
console.log(_.clamp(10, -5, 5)); // 5
Try It Yourself

How It Works

  1. Value. 10 is above 5.
  2. Cap. Lowered to the upper bound.
  3. Result. 5 — matches the official docs.
  4. Pair. Same range as Example 1.

Example 3 — Value Already Inside the Range

In-range numbers pass through unchanged.

Lodash
console.log(_.clamp(0, -5, 5));  // 0
console.log(_.clamp(-5, -5, 5)); // -5
console.log(_.clamp(5, -5, 5));  // 5
Try It Yourself

How It Works

  1. Middle. 0 stays 0.
  2. Edges. Exact bounds are kept (inclusive).
  3. No change. Clamping only moves out-of-range values.
  4. Use. Safe to call on every input.

Example 4 — Clamp a Percent to 0–100

A common UI pattern for progress and volume.

Lodash
function toPercent(n) {
  return _.clamp(n, 0, 100);
}

console.log(toPercent(-3));  // 0
console.log(toPercent(42));  // 42
console.log(toPercent(150)); // 100
Try It Yourself

How It Works

  1. Range. Valid percents are 0 to 100.
  2. Too low. -3 → 0.
  3. OK. 42 stays.
  4. Too high. 150 → 100.

Example 5 — Normalize Bounds Before Clamping

When users may swap min/max, order them first.

Lodash
function safeClamp(n, a, b) {
  var lower = Math.min(a, b);
  var upper = Math.max(a, b);
  return _.clamp(n, lower, upper);
}

console.log(safeClamp(5, 10, 0)); // 5
console.log(_.clamp(5, 10, 0));   // 10  (reversed — surprising)
Try It Yourself

How It Works

  1. Normalize. Math.min / Math.max fix order.
  2. safeClamp. 5 stays inside [0, 10].
  3. Raw clamp. Reversed bounds are not swapped.
  4. Rule. Always pass lower ≤ upper.

Browser & Runtime Support

_.clamp is a Lodash Number helper available since 4.0.0. It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native engines also offer Math.min(upper, Math.max(lower, n)) when bound order is known.

✓ Stable · Lodash 4.x

_.clamp()

Available in [email protected] full builds and the lodash/clamp modular import. Works in evergreen browsers and current Node LTS 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
_.clamp Since 4.0.0

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

Frequently Asked Questions

_.clamp(number, lower, upper) clamps number within the inclusive lower and upper bounds. Official docs: _.clamp(-10, -5, 5) → -5 and _.clamp(10, -5, 5) → 5.
It is returned unchanged. For example _.clamp(0, -5, 5) → 0.
Lodash does not swap them. Bounds are applied as given (upper first, then lower), so reversed arguments can surprise you. Normalize with Math.min / Math.max when order is unknown.
Only when lower ≤ upper. Prefer Math.min/Math.max on the bounds first if users can pass them in either order.
_.clamp returns a number forced into the range. _.inRange returns a boolean — whether a value sits in a half-open range.
Use import clamp from "lodash/clamp" or require("lodash/clamp") so bundlers can tree-shake. Full build: import _ from "lodash" then _.clamp.

Next: _.inRange()

Check whether a number falls inside a start/end range.

_.inRange() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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