JavaScript — How to create and fill an empty array with a given size?

All About Code
Nov 17, 2022

Solution

const arr = Array(5).fill("");
console.log(arr); //prints out ["","","","",""]
console.log(arr[0]); //prints out ""

1) To create new array which, you cannot iterate over, you can use array constructor:

Array(100) or new Array(100)

2) You can create new array, which can be iterated over like below:

a) All JavaScript versions

  • Array.apply: Array.apply(null, Array(100))

--

--