Constrains a given number n between a specified minimum (low) and maximum (high) value.
n
low
high
If n is less than low, the function returns low. If n is greater than high, the function returns high. Otherwise, it returns n unchanged.
The number to constrain.
The minimum allowable value.
The maximum allowable value.
The constrained value of n, which will be in the range [low, high].
[low, high]
const constrained1 = constrain(5, 1, 10); // Returns 5 (within range)const constrained2 = constrain(-5, 1, 10); // Returns 1 (below range)const constrained3 = constrain(15, 1, 10); // Returns 10 (above range) Copy
const constrained1 = constrain(5, 1, 10); // Returns 5 (within range)const constrained2 = constrain(-5, 1, 10); // Returns 1 (below range)const constrained3 = constrain(15, 1, 10); // Returns 10 (above range)
Constrains a given number
n
between a specified minimum (low
) and maximum (high
) value.If
n
is less thanlow
, the function returnslow
. Ifn
is greater thanhigh
, the function returnshigh
. Otherwise, it returnsn
unchanged.