_.clamp(number, [lower], upper)

Clamps number within the inclusive lower and upper bounds.

Since

4.0.0

Arguments

  1. number (number): The number to clamp.
  2. [lower] (number): The lower bound.
  3. upper (number): The upper bound.

Returns

(number): Returns the clamped number.

Example

  1. _.clamp(-10, -5, 5);
  2. // => -5
  3. _.clamp(10, -5, 5);
  4. // => 5

_.inRange(number, [start=0], end)

Checks if n is between start and up to, but not including, end. If end is not specified, it’s set to start with start then set to 0. If start is greater than end the params are swapped to support negative ranges.

Since

3.3.0

Arguments

  1. number (number): The number to check.
  2. [start=0] (number): The start of the range.
  3. end (number): The end of the range.

Returns

(boolean): Returns true if number is in the range, else false.

Example

  1. _.inRange(3, 2, 4);
  2. // => true
  3. _.inRange(4, 8);
  4. // => true
  5. _.inRange(4, 2);
  6. // => false
  7. _.inRange(2, 2);
  8. // => false
  9. _.inRange(1.2, 2);
  10. // => true
  11. _.inRange(5.2, 4);
  12. // => false
  13. _.inRange(-3, -2, -6);
  14. // => true

_.random([lower=0], [upper=1], [floating])

Produces a random number between the inclusive lower and upper bounds. If only one argument is provided a number between 0 and the given number is returned. If floating is true, or either lower or upper are floats, a floating-point number is returned instead of an integer.

Note: JavaScript follows the IEEE-754 standard for resolving floating-point values which can produce unexpected results.

Since

0.7.0

Arguments

  1. [lower=0] (number): The lower bound.
  2. [upper=1] (number): The upper bound.
  3. [floating] (boolean): Specify returning a floating-point number.

Returns

(number): Returns the random number.

Example

  1. _.random(0, 5);
  2. // => an integer between 0 and 5
  3. _.random(5);
  4. // => also an integer between 0 and 5
  5. _.random(5, true);
  6. // => a floating-point number between 0 and 5
  7. _.random(1.2, 5.2);
  8. // => a floating-point number between 1.2 and 5.2