Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptLodash _.range() Method

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:

  • start: It is a number that specifies the start of the range. It is an optional value. The default value is 0.
  • end: It is a number that specifies the end of the range.
  • step: It is a number that specifies the amount that the value in the range is incremented or decremented. The default value is 1.

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.

Javascript




// 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.

Javascript




// 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.

Javascript




// 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]

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments