Categories
JavaScript Answers

How to create an array with same element repeated multiple times with JavaScript?

Spread the love

To create an array with same element repeated multiple times with JavaScript, we call the fill method.

For instance, we write

const arr = Array(15).fill(2);

to create an empty array with 15 slots with Array.

And then we call fill to fill all the slots with 2’s.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.