Skip to content

clamp

Restricts a value to a range defined by minimum and maximum limits.

Syntax

typescript
clamp(value: number, min: number, max: number): number

Parameters

NameTypeDescription
valuenumberThe value to clamp.
minnumberMinimum allowed value.
maxnumberMaximum allowed value.

Return

TypeDescription
numberThe clamped value within the range.

Example

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

Notes

  • Throws TypeError if any argument is not a number.
  • Throws RangeError if min is greater than max.

References

Released under the MIT License.