Home > shorts > Coding range() function with plain JavaScript

Coding range() function with plain JavaScript


Coding range() function with plain JavaScript

There is no built in range() method in JavaScript. Let's code it!

function range(startAt, endAt) {
  const size = endAt - startAt + 1
  return [...Array(size).keys()].map(i => i + startAt);
}

range( 0, 5 ) // [0, 1, 2, 3, 4, 5]

Check the demo here => CodePen

© 2021, Andrew Losseff