Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 678 Bytes

clamp-a-number-between-two-values.mdx

File metadata and controls

31 lines (23 loc) · 678 Bytes
category contributors created title updated
Number
r-i-c-h
2020-05-05
Clamp a number between two values
2021-10-13

JavaScript version

const clamp = (val, min = 0, max = 1) => Math.max(min, Math.min(max, val));

TypeScript version

const clamp = (val: number, min: number = 0, max: number = 1): number => Math.max(min, Math.min(max, val));

Examples

clamp(199, 10, 25); // 25

See also