Math.clamp
Constrain a number within a given range.
| Type | TC39 | CoreJS | Babel | Civet | JS+ |
|---|---|---|---|---|---|
| Polyfill | ✅ Stage 2 View | ✅ View | ❌ | ❌ | ✅ via Custom |
Math.clamp(value: number, min: number, max: number): number
Section titled “Math.clamp(value: number, min: number, max: number): number”Returns value if it lies within [min, max], min if it is below, or max if it is above.
Math.clamp(5, 1, 10); // → 5 (within range)Math.clamp(0, 1, 10); // → 1 (below min)Math.clamp(99, 1, 10); // → 10 (above max)
Math.clamp(10, 10, 10); // → 10 (min, value, and max are equal)
Math.clamp(5, 10, 1); // ✗ RangeError: min (10) must not be greater than max (1)