Skip to main content

clamp

The clamp() function clamps a value between a minimum and maximum value.

Parameters

ParameterTypeDescription
valueNumberThe value to be clamped.
minNumberThe minimum value that value can be.
maxNumberThe maximum value that value can be.

Returns

  • Number: The clamped value

Throws

  • Error: Will throw an error if value, min, or max is not a number.

Example

at velue that is less than min

const utils = require('utils-core.js');
const current = 5;
const min = 10;
const max = 50;
const clampedValue = utils.math.clamp(current, min, max);
console.log(clampedValue);
//output: 10

at velue that is greater than max

const utils = require('utils-core.js');
const current = 100;
const min = 10;
const max = 50;
const clampedValue = utils.math.clamp(current, min, max);
console.log(clampedValue);
//output: 50