Categories
JavaScript Answers

How to set all values of an array with JavaScript?

Spread the love

To set all values of an array with JavaScript, we use the fill method.

For instance, we write

const arr = new Array(3).fill(9);

to create an empty array with length 3 with Array(3).

Then we call fill with 9 to fill the empty slots with 9’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. Required fields are marked *