Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Lodash _.rangeRight() Method

  • Last Updated : 09 Sep, 2020

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

The _.rangeRight() method is used to create an array of numbers progressing from the given start value to the given, but not including, the given end value. It populates the values in descending order. A step value of -1 is used if a negative start is specified without an end or step. If the end is not specified, it’s set to start with the start then set to 0.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Syntax:

_.rangeRight( start, end, step )

Parameters: This method accepts three parameters as mentioned above and described below:



  • 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 range of numbers in descending order.

Example 1:

Javascript




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

Output:

[5, 4, 3, 2, 1, 0]

Example 2:

Javascript




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

Output:

[12, 9, 6, 3, 0]

Example 3:

Javascript




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

Output:

[-8, -6, -4, -2, 0]



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!