Open In App

Lodash _.range() Method

Lodash _.range() method is used to create an array of numbers progressing from the given start value up to, but not including the end value. A step of -1 is used if a negative value of start is specified without an end or step. If the end is not specified, it’s set to start with the start and then set to 0.

Syntax:

_.range([start=0], end, [step=1]);

Parameters:

Return Value:

It returns an array with the given range of numbers.



Example 1: In this example, we are printing 5 values in the console as we are passing 5 as an end parameter to the _.range() method.




// Requiring the lodash library 
const _ = require("lodash");           
   
// Using the _.range() method
let range_arr = _.range(5);
       
// Printing the output 
console.log(range_arr);

Output:



[0, 1, 2, 3, 4]

Example 2: In this example, we are passing 0 as a start, 10 as an end and 2 as step and passing these parameter to the _.range() method.




// Requiring the lodash library 
const _ = require("lodash");
 
// Using the _.range() method
// with the step taken as 2
let range_arr = _.range(0, 10, 2);
 
// Printing the output 
console.log(range_arr);

Output:

[0, 2, 4, 6, 8]

Example 3: In this example, we are passing -1 as a start, -11 as an end and -2 as step and passing these parameter to the _.range() method.




// Requiring the lodash library 
const _ = require("lodash");
 
// Using the _.range() method
// with the step taken as -2
let range_arr = _.range(-1, -11, -2);
 
// Printing the output 
console.log(range_arr);

Output:

[-1, -3, -5, -7, -9]

Article Tags :