Published at
Updated at
Reading time
1min
This post is part of my Today I learned series in which I share all my web development learnings.

I followed a Twitter conversation between Surma from Google and Andrea Giammarchi in which Andrea mentionied that Array.from accepts a second argument. 😲

console.log(Array.from([1, 2, 3], x => x + x));
// [2, 4, 6]

The snippet they discussed showed functionality to call a callback X times.

// call a callback `length` times
Array.from({length}, () => callback())

// or generate a random emoji Array
Array.from(
  {length: 7},
  (v, i) => String.fromCodePoint(
    129300 + Math.floor(Math.random() * 20)
  )
);

// [ '🤡', '🤗', '🤥', '🤛', '🤤', '🤦', '🤔' ]

You can read more about Array.from on MDN.

If you enjoyed this article...

Join 5.5k readers and learn something new every week with Web Weekly.

Web Weekly — Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles