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): numberParameters
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:minifvalue<minmaxifvalue>maxvalueif it's betweenminandmax
Example Usage
playground.ts
Notes
- Order Matters: If
min>max, the function will returnmin(due to the internal structure ofMath.max(min, Math.min(value, max))). - Type Safety: Only accepts
numbertype. - Performance: Uses native
Math.maxandMath.minfor optimal performance.
Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)
