Skip to content

Function: clamp() ​

clamp(value, min, max): number

Defined in: math.ts:37

Clamps a number between a minimum and maximum value. If value is below min, returns min. If above max, returns max. Otherwise returns value unchanged.

Parameters ​

value ​

number

The number to clamp.

min ​

number

The lower bound.

max ​

number

The upper bound.

Returns ​

number

The clamped value.

Example ​

ts
clamp(5, 0, 10)       // 5 (within range, unchanged)
clamp(-3, 0, 10)      // 0 (below min)
clamp(15, 0, 10)      // 10 (above max)
clamp(0.5, 0, 1)      // 0.5
clamp(-0.5, 0, 1)     // 0
clamp(1.5, 0, 1)      // 1