⚠️ This lesson is retired and might contain outdated information.

4 Ways to populate an Array in Javacript

Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 2 years ago

Javascript offers at least 4 ways to fill an array with different values using only native language methods. These methods allow you to create an arrangement of a predefined size and content.

It's possible to use Array.fill to create an Array with a pre-defined size and then use Array.map to populate the items with a default value, like integer or string.

It's also possible to populate the array with an object, but we will require to use another function because Array.fill just copy a reference to the original object without creating multiple unique objects. To solve this we can use Array.map.

Other way to solve the same problem is by using a for loop, but this is a mutable method.

Also, we can use Array.from or the array spread syntax in combination with the Array.map function.

egghead
egghead
~ 7 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today