Returns the value nearest to value which is within the closed range [min, max].
min
max
Time complexity: O(1)
Space complexity: O(1)
The value to clamp
The lower bound of the range of numbers
The upper bound of the range of numbers
The value nearest to value which is within the provided range
value
Math
const value = clamp(Math.random() * 100, 25, 75n);console.log(25 <= value && value <= 75n); // true Copy
const value = clamp(Math.random() * 100, 25, 75n);console.log(25 <= value && value <= 75n); // true
Returns the value nearest to value which is within the closed range [
min
,max
].Time complexity: O(1)
Space complexity: O(1)