Open In App

Lodash _.rangeRight() Method

Last Updated : 13 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Lodash _.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.

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: In this example, the code utilizes the Lodash library’s _.rangeRight method to generate an array of numbers in reverse order and displays the result in the console.

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: In this example, the code uses Lodash’s _.rangeRight method to create an array of numbers in reverse order, starting from 15 and decreasing by 3, and then it prints the resulting array in the console.

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: In this example, the code requires the Lodash library and utilizes the _.rangeRight method with a step of -2 to generate an array of numbers in reverse order, starting from 0 and decreasing by 2, and then displays the result in the console.

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]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads