The _.inRange() method takes a number, and checks to see if it is between given start and end parameters. If the end is not specified, then it is set equals to the start, and then start is set equals 0. If the start is greater than the end the parameters are swapped to support negative ranges.
Syntax:
_.inRange(number, start, end)
Parameters: This method accepts three parameters as mentioned above and described below:
- number: This parameter holds the number to check.
- start: This parameter holds the start value of the range.
- end: This parameter holds the end value of the range.
Return Value: This method returns true if the number is in the range, else false.
Example 1:
Javascript
// Requiring the lodash library const _ = require( "lodash" ); // Use of _.inRange method console.log(_.inRange(12, 10)); console.log(_.inRange(10, 12)); console.log(_.inRange(5.6, 5)); console.log(_.inRange(5.6, 6)); |
Output:
false true false true
Example 2:
Javascript
// Requiring the lodash library const _ = require( "lodash" ); // Use of _.inRange method console.log(_.inRange(2, 3, 5)); console.log(_.inRange(2, 2, 4)); console.log(_.inRange(4, 2, 4)); console.log(_.inRange(-2, -1, -5)); |
Output:
false true false true