Categories
JavaScript Answers

How to set default array values with JavaScript?

Spread the love

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

For instance, we write

const a = Array(20).fill(0);

to create an empty array with 20 slots with Array.

Then we call fill to fill all the empty slots with 0.

Conclusion

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

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.