MENU
    minMax
    • 09 Apr 2021
    • 1 Minute to read
    • Contributors
    • Dark
    • PDF

    minMax

    • Dark
    • PDF

    Article summary

    Description

    Checks the specified value and makes sure it is within the specified range. If less than the minimum, then the minimum is returned. If greater than the maximum, then the maximum is returned. Otherwise, the specified value is returned.


    Input Parameters

    NameTypeRequiredDescription
    valueNumberYesValue to check if within range
    minNumberYesMinimum allowed value in range
    maxNumberYesMaximum allowed value in range

    Returns

    Type: Number

    Checks if the passed value is within range and returns the appropriate value.


    Examples

    // checks to see if x is within the range of 10 to 20
    // since true, 13 is returned
    var x = 13;
    x = tools.minMax(x, 10, 20);
    JavaScript
    // since false, 10 is returned
    var x = 3;
    x = tools.minMax(x, 10, 20);
    JavaScript
    // since false, 20 is returned
    var x = 147;
    x = tools.minMax(x, 10, 20);
    JavaScript

    Remarks

    Use this helper function when you want to restrict a value within a certain range. This allows you to perform this restriction within a single line of code.


    What's Next