r/javascript • • Mar 18 '19

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

385 Upvotes

422 comments sorted by

View all comments

7

u/action_jackosn Mar 19 '19

Array.from({length: 5}, (v, i) => i); := [0, 1, 2, 3, 4]

13

u/Darren1337 Mar 19 '19

I'm about to end this man's whole career.

[...Array(5).keys()]

2

u/NoInkling Mar 19 '19

I can't think of a use case where I'd want to materialize an array of numbers equivalent to their index - I'd just use the map function directly.

1

u/[deleted] Mar 19 '19

Array.from({ length: 5 }, (_, i) => i);

for unused variable

2

u/snifty Mar 19 '19

This is neat. I've usually done:

new Array(5).fill().map((_,i) => i)

I like your way better.