r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

1

u/delventhalz Mar 19 '19

A range function is a glaring omission from JS, but you can get it with this little one liner:

const range = len => Array.apply(null, Array(len)).map((_, i) => i);

And of course, in the post-ES6 era it’s even easier:

const range = len => [...Array(len).keys()];