1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    4 Ways to populate an Array in Javacript

    javascriptJavaScript

    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.