category | contributors | created | title | updated | |
---|---|---|---|---|---|
Number |
|
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