Clamp a number between two values
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