The range function of Python implemented using JavaScript.
range(start, end, step)
npm i jrange -S
- in browser
<script src="/dist/jrange.umd.js"></script>
<script>
const list = jrange(10);
</script>
- esm
import jrange from 'jrange';
const list = jrange(10, 1, -1);
- CommonJS
const range = require('jrange'); // rename to range
const list = range(1, 100, 2);
/ | default | required |
---|---|---|
start | 0 | no |
end | / | yes |
step | 1 | no |
range(10)
-> range(0, 10, 1)
range(1, 10)
-> range(1, 10, 1)
- In Python, the range function returns a
Range Class
, while jrange returns anArray
.
python
rng = range(0, 10)
print(rng, type(rng))) # range(0, 10) <class 'range'>
# Convert to list
list = list(rng)
javascript
const list = jrange(0, 10)
console.log(Array.isArray(list)) // true
- In Python, if the step is zero, the range function will throw an exception, while jrange will return an empty array.
python
range(1, 10, 0)
# ValueError: range() arg 3 must not be zero
javascript
jrange(1, 10, 0); // return []