• Category

    Returns the value nearest to value which is within the closed range [min, max].

    Time complexity: O(1)

    Space complexity: O(1)

    Type Parameters

    • T extends number | bigint
    • M1 extends number | bigint
    • M2 extends number | bigint

    Parameters

    • value: T

      The value to clamp

    • min: M1

      The lower bound of the range of numbers

    • max: M2

      The upper bound of the range of numbers

    Returns T | M1 | M2

    The value nearest to value which is within the provided range

    Math

    Example

    const value = clamp(Math.random() * 100, 25, 75n);

    console.log(25 <= value && value <= 75n); // true