const range = (from, to, step) => [...Array(Math.floor((to - from) / step) + 1)].map((_, i) => from + i * step); range(0, 9, 2); //=> [0, 2, 4, 6, 8] // can also assign range function as static method in Array class (but not recommended ) Array.range = (from, to, step) => [...Array(Math.floor((to - from) / step) + 1)].map((_, i) => from + i * step); Array.range(2, 10, 2); //=> [2, 4, 6, 8, 10] Array.range(0, 10, 1); //=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Array.range(2, 10, -1); //=> [] Array.range(3, 0, -1); //=> [3, 2, 1, 0]
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter