In order to create arrays in non literal form we use the array object construction together with the fill method or the static from function of the Array object to initiate array size and mapping callback to populate the array.
/* Create a new array and populate it */ const newArr = new Array(10); /* the signature is .fill(value, startPos, finalPos), excluding finalPos */ newArr.fill(3, 4, 7) /* will print [empty*4, 3, 3, 3, empty*3] */ /* Create a new array using the Array class */ const newArr2 = Array.from({ length: 10 }, (_, i) => i ** 2); /* Will print an array with elements values as index number squared */ console.log(newArr2);
Notice that Array.from allows any iterable to be used for new array construction, and so the callback used to populate the entries is malleable.