For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/math/clamp.md.

clamp es-toolkit

Clamps a number within a specified range.

const clamped = clamp(value, maximum);
const clamped = clamp(value, minimum, maximum);

Usage

clamp(value, maximum)

Use clamp when you want to restrict a number to be at most a given maximum value. If the value exceeds the maximum, it's clamped to the maximum; otherwise, it returns the original value.

import { clamp } from 'es-toolkit/math';

// Clamp to maximum value
const result1 = clamp(10, 5); // result1 is 5 (10 is clamped to maximum 5)
const result2 = clamp(3, 5); // result2 is 3 (less than 5, so unchanged)

Parameters

valuenumberrequired

The number to clamp.

maximumnumberrequired

The maximum value.

Returns

Returns the number clamped to be at most the maximum value.

number

clamp(value, minimum, maximum)

Use clamp when you want to restrict a number within a given minimum and maximum range. If the value falls outside the range, it's clamped to the nearest boundary.

import { clamp } from 'es-toolkit/math';

// Clamp within minimum and maximum range
const result1 = clamp(10, 5, 15); // result1 is 10 (within range 5-15)
const result2 = clamp(2, 5, 15); // result2 is 5 (clamped to minimum 5)
const result3 = clamp(20, 5, 15); // result3 is 15 (clamped to maximum 15)

Parameters

valuenumberrequired

The number to clamp.

minimumnumberrequired

The minimum value.

maximumnumberrequired

The maximum value.

Returns

Returns the number clamped within the specified range.

number
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.