I ran into the following:
// Nothing is printed to console
Array(10).forEach(() => console.log("hi"));
// "hi" is printed 10 times
Array(10).fill(0).forEach(() => console.log("hi"));
Looking at Array constructor docs, the 10
passed in corresponds to the parameter arrayLength
with the following documentation:
this returns a new JavaScript array with its length property set to that number (Note: this implies an array of
arrayLength
empty slots, not slots with actualundefined
values)
I'm assuming length
property indicates how much memory to allocate for the array, but what is an "empty slot" (vs. a slot with undefined
or some default value)?