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/psayre23 Mar 19 '19

Create an array of the same items...

const fill = (item, length) => Array.from({length}).map(() => item);

const ones = fill(1, 45);

Or creating an array with a range of numbers...

const range = (start, end) => Array.from({length: end - start}).map((_, i) => start + i);

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

Edit: formatting