num
within the inclusive range specified by the boundary values a
and b
.num
falls within the range, return num
.const clampNumber = (num, a, b) =>
Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));
clampNumber(2, 3, 5); // 3 clampNumber(1, -1, -5); // -1
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️