Open In App

Lodash _.inRange() Method

Lodash _.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 equal to the start, and then the start is set equal to 0. If the start is greater than the end the parameters are swapped to support negative ranges.

Syntax:

_.inRange(number, start, end);

Parameters:

Return Value:

This method returns true if the number is in the range, else false.



Example 1: In this example, we are checking whether the given value is present between the given start and end number, also start number is inclusive.




// 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: In this example, we are checking whether the given value is present between the given start and end number and printing the result in the console.




// 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
Article Tags :