Toolbox-XToolbox-X

Clamp Number

Restricts a number to fall within a specified minimum and maximum range.

clampNumber

The clampNumber function restricts a number to fall within a specified range. If the number is lower than the minimum, it returns the minimum; if higher than the maximum, it returns the maximum; otherwise, it returns the original number.

Function Signature

function clampNumber(value: number, min: number, max: number): number

Parameters

  • value (number): The number to be clamped.
  • min (number): The lower bound of the range (inclusive).
  • max (number): The upper bound of the range (inclusive).

Return Value

  • number:
    • min if value < min
    • max if value > max
    • value if it's between min and max

Example Usage

playground.ts

Notes

  • Order Matters: If min > max, the function will return min (due to the internal structure of Math.max(min, Math.min(value, max))).
  • Type Safety: Only accepts number type.
  • Performance: Uses native Math.max and Math.min for optimal performance.

Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)

On this page