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:
- from: This parameter holds the starting value of the range.
- to: This parameter holds the ending value of the range.
- by: This parameter holds the gap between to continues value in the range.
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.
Javascript
// 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:
Javascript
// 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...