JavaScript: make arrays of size | how to create filler arrays of varying length

Fill long arrays with content. There are times when you need arrays filled with nothing particular.

Push into the array

Maybe the most obvious way is to fill the array with a while loop (or for loop would do just as well):

let count = 0
const arr = []
 
while (count < 1000) {
  arr.push(count)
  count++
}
 
console.log(arr)

Fill arrays with the array constructor

If you call the array constructor function with an integer as a param, it will give you an empty array of n length:

const emptyArray = new Array(100)
console.log(emptyArray)
Empty array with a length of 100
[empty x 100] array

If you loop the empty array, the values will be undefined:

for (const arrayItem of emptyArray) {
  console.log(arrayItem) // undefined
}

Filling arrays with fill()

After youโ€™ve created your empty array, it can be filled with values using the fill method, in this case just with zeros:

const filledArray = new Array(100).fill(0)

Packing arrays with Array.from()

With Array.from() you can make an array from anything that is iterable, a string for example:

Array.from('foo') // [f, o, o]

Or you can configure its length:

Array.from({ length: 4 }) // [undefined, undefined, undefined, undefined]

The Array.from() method can take a second parameter: a function, with which you can map values into to the empty array:

Array.from({ length: 5 }, (x, i) => i) // [1, 2, 3, 4, 5]

It can also take an array as its first argument, and then further map values into that array:

const existingArray = [100, 200, 300, 400, 500]
Array.from(existingArray, (x, i) => x + i) // [100, 201, 302, 403, 504]

A few examples

Below are a few nifty examples that might come in handy.

Fill array with random numbers

The content can be anything, random numbers for example:

const getRandomInt = max => Math.floor(Math.random() * max)
const randomNumbers = Array.from({ length: 1000 }, () => getRandomInt(100))

That would be a list of 1000 random integers, with a max size of 100.

Fill array with ranges, like the emoji unicode ranges

The Array.from() method is pretty cool, for example you can define a 2D array of emoji ranges, then map that outer dimension and process the innards with Array.from(), then flat it, and youโ€™ve got a nice array of emoji, only with few lines of code:

const emojiRange = [
  [128513, 128591],
  [128640, 128704],
  [9986, 10160],
]
 
const emojis = emojiRange
  .map(([start, end]) =>
    Array.from({ length: end - start }, (x, i) => i + start)
  )
  .flat()

You can then render the emoji with React for example using String.fromCodePoint():

<div>
  {emojis.map(emoji => (
    <span title={`&#${emoji};`}>{String.fromCodePoint(emoji)}</span>
  ))}
</div>

It produces all the emoji and dingbats:

๐Ÿ˜๐Ÿ˜‚๐Ÿ˜ƒ๐Ÿ˜„๐Ÿ˜…๐Ÿ˜†๐Ÿ˜‡๐Ÿ˜ˆ๐Ÿ˜‰๐Ÿ˜Š๐Ÿ˜‹๐Ÿ˜Œ๐Ÿ˜๐Ÿ˜Ž๐Ÿ˜๐Ÿ˜๐Ÿ˜‘๐Ÿ˜’๐Ÿ˜“๐Ÿ˜”๐Ÿ˜•๐Ÿ˜–๐Ÿ˜—๐Ÿ˜˜๐Ÿ˜™๐Ÿ˜š๐Ÿ˜›๐Ÿ˜œ๐Ÿ˜๐Ÿ˜ž๐Ÿ˜Ÿ๐Ÿ˜ ๐Ÿ˜ก๐Ÿ˜ข๐Ÿ˜ฃ๐Ÿ˜ค๐Ÿ˜ฅ๐Ÿ˜ฆ๐Ÿ˜ง๐Ÿ˜จ๐Ÿ˜ฉ๐Ÿ˜ช๐Ÿ˜ซ๐Ÿ˜ฌ๐Ÿ˜ญ๐Ÿ˜ฎ๐Ÿ˜ฏ๐Ÿ˜ฐ๐Ÿ˜ฑ๐Ÿ˜ฒ๐Ÿ˜ณ๐Ÿ˜ด๐Ÿ˜ต๐Ÿ˜ถ๐Ÿ˜ท๐Ÿ˜ธ๐Ÿ˜น๐Ÿ˜บ๐Ÿ˜ป๐Ÿ˜ผ๐Ÿ˜ฝ๐Ÿ˜พ๐Ÿ˜ฟ๐Ÿ™€๐Ÿ™๐Ÿ™‚๐Ÿ™ƒ๐Ÿ™„๐Ÿ™…๐Ÿ™†๐Ÿ™‡๐Ÿ™ˆ๐Ÿ™‰๐Ÿ™Š๐Ÿ™‹๐Ÿ™Œ๐Ÿ™๐Ÿ™Ž๐Ÿš€๐Ÿš๐Ÿš‚๐Ÿšƒ๐Ÿš„๐Ÿš…๐Ÿš†๐Ÿš‡๐Ÿšˆ๐Ÿš‰๐ŸšŠ๐Ÿš‹๐ŸšŒ๐Ÿš๐ŸšŽ๐Ÿš๐Ÿš๐Ÿš‘๐Ÿš’๐Ÿš“๐Ÿš”๐Ÿš•๐Ÿš–๐Ÿš—๐Ÿš˜๐Ÿš™๐Ÿšš๐Ÿš›๐Ÿšœ๐Ÿš๐Ÿšž๐ŸšŸ๐Ÿš ๐Ÿšก๐Ÿšข๐Ÿšฃ๐Ÿšค๐Ÿšฅ๐Ÿšฆ๐Ÿšง๐Ÿšจ๐Ÿšฉ๐Ÿšช๐Ÿšซ๐Ÿšฌ๐Ÿšญ๐Ÿšฎ๐Ÿšฏ๐Ÿšฐ๐Ÿšฑ๐Ÿšฒ๐Ÿšณ๐Ÿšด๐Ÿšต๐Ÿšถ๐Ÿšท๐Ÿšธ๐Ÿšน๐Ÿšบ๐Ÿšป๐Ÿšผ๐Ÿšฝ๐Ÿšพ๐Ÿšฟโœ‚โœƒโœ„โœ…โœ†โœ‡โœˆโœ‰โœŠโœ‹โœŒโœโœŽโœโœโœ‘โœ’โœ“โœ”โœ•โœ–โœ—โœ˜โœ™โœšโœ›โœœโœโœžโœŸโœ โœกโœขโœฃโœคโœฅโœฆโœงโœจโœฉโœชโœซโœฌโœญโœฎโœฏโœฐโœฑโœฒโœณโœดโœตโœถโœทโœธโœนโœบโœปโœผโœฝโœพโœฟโ€โโ‚โƒโ„โ…โ†โ‡โˆโ‰โŠโ‹โŒโโŽโโโ‘โ’โ“โ”โ•โ–โ—โ˜โ™โšโ›โœโโžโŸโ โกโขโฃโคโฅโฆโงโจโฉโชโซโฌโญโฎโฏโฐโฑโฒโณโดโตโถโทโธโนโบโปโผโฝโพโฟโž€โžโž‚โžƒโž„โž…โž†โž‡โžˆโž‰โžŠโž‹โžŒโžโžŽโžโžโž‘โž’โž“โž”โž•โž–โž—โž˜โž™โžšโž›โžœโžโžžโžŸโž โžกโžขโžฃโžคโžฅโžฆโžงโžจโžฉโžชโžซโžฌโžญโžฎโžฏ

Hereโ€™s a CodeSandbox of a vanilla JS example (itโ€™s almost the same, tho):

Edit fill-arrays-with-emoji

Conclusions

2ality in his article goes deeper into the nitty-gritty of array creation, if youโ€™re interested in the underlying V8 parser engine behavior etc.