Open In App

Underscore.js _.iterators.range() Method

With the help of _.iterators.range() method, we can get the iterator function that gives the value within a given range which is specified into arguments and got incremented whenever invoked by using this method.

Syntax:



_.iterators.range(from, to, by)

Parameter: This method accepts three parameter as mentioned above and described below: 

Return: Return the value within a given range.



Note: To execute the below examples you have to install the underscore-contrib library by using this command prompt we have to execute the following command.

npm install underscore-contrib

Below example illustrate the Underscore.js _.iterators.range() method in JavaScript: 

Example 1: In this example, we can see that by using _.iterators.range() method, we are able to get the values from iterator function who given the incremented value every time whenever invoked.




// Defining underscore contrib variable
var _ = require('underscore-contrib');
 
var geek = _.iterators.range(15, Infinity, 5);
 
for(var i = 0; i < 5; i++) {
    console.log(geek());
}

Output:

15
20
25
30
35...

Example 2:




// Defining underscore contrib variable
var _ = require('underscore-contrib');
 
var geek = _.iterators.range(5, Infinity, 2);
 
for(var i = 0; i < 3; i++) {
    console.log(geek());
}

Output:

5
7
9...
Article Tags :