_.range

Creates an array of numbers progressing from start up to.

  1. // Underscore/Lodash
  2. _.range(4) // output: [0, 1, 2, 3]
  3. _.range(-4) // output: [0, -1, -2, -3]
  4. _.range(1, 5) // output: [1, 2, 3, 4]
  5. _.range(0, 20, 5) // output: [0, 5, 10, 15]
  6.  
  7. // Native ( solution with Array.from )
  8. Array.from({length: 4}, (_, i) => i) // output: [0, 1, 2, 3]
  9. Array.from({length: 4}, (_, i) => -i) // output: [-0, -1, -2, -3]
  10. Array.from({length: 4}, (_, i) => i + 1) // output: [1, 2, 3, 4]
  11. Array.from({length: 4}, (_, i) => i * 5) // output: [0, 5, 10, 15]
  12.  
  13. // Native ( solution with keys() and spread )
  14. [...Array(4).keys()] // output: [0, 1, 2, 3]
  15. [...Array(4).keys()].map(k => -k) // output: [-0, -1, -2, -3]
  16. [...Array(4).keys()].map(k => k + 1) // output: [1, 2, 3, 4]
  17. [...Array(4).keys()].map(k => k * 5) // output: [0, 5, 10, 15]

Browser Support for Array.from()

ChromeEdgeFirefoxIEOperaSafari
45.0 ✔32.0 ✔9.0 ✔

Browser Support for keys and spread in Array literals

ChromeEdgeFirefoxIEOperaSafari
46.0 ✔12.0 ✔16.0 ✔37.0 ✔7.1 ✔