Skip to content

ensureBounds() ​

ts
function ensureBounds(
   this, 
   min, 
   max): number;

Defined in: number/bounds.ts:18

Clamps a number to ensure it stays within the provided [min, max] range`.

Parameters ​

ParameterTypeDescription
thisnumbernumber instance
minnumberlower bound (inclusive)
maxnumberupper bound (inclusive)

Returns ​

number

number that is guaranteed to be between min and max (inclusive), unless this coerces to NaN (see edge cases)

Example ​

ts
const boundedNumberMax = (100).ensureBounds(1, 50);
const boundedNumberMin = (0).ensureBounds(1,50);
console.assert(boundedNumberMax === 50);
console.assert(boundedNumberMin === 1);

Since ​

1.0.0

Author ​

Simon Kovtyk