Categories
JavaScript Answers

How to add default array values with JavaScript?

Spread the love

To add default array values with JavaScript, we call the fill method.

For instance, we write

const arr = Array(100).fill(0);

to define an array with 100 empty slots with Array(100).

Then we call fill with 0 to fill the empty slots with 0’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 *