Skip to content

@technobuddha > library > Math > Operations

Function: clamp()

ts
function clamp(
   value: number, 
   min: number, 
   max: number): number;

Defined in: clamp.ts:17

Clamps a number within the inclusive range specified by min and max.

Parameters

ParameterTypeDescription
valuenumberThe number to clamp.
minnumberThe lower bound of the range.
maxnumberThe upper bound of the range.

Returns

number

The clamped value, which will be no less than min and no greater than max.

Example

typescript
clamp(5, 1, 10); // 5
clamp(-2, 0, 7); // 0
clamp(15, 0, 10); // 10
clamp(3, 3, 3); // 3