Initialize an array in JavaScript
javascript
Updated August 29, 2026
Choose the initializer based on whether the values are independent or the same reference:
const zeros = Array(4).fill(0);
const indexes = Array.from({ length: 4 }, (_, i) => i);
Array(4) creates four empty slots; methods such as map() skip those slots. fill() writes a value into every position, which is fine for primitives. With objects, every position receives the same reference:
const shared = Array(3).fill({ done: false });
shared[0].done = true; // all three entries now refer to done: true
Create separate objects with Array.from({ length: 3 }, () => ({ done: false })). A literal such as [1, 2, 3] is clearest for a short fixed list. Keep the array's intended shape explicit rather than using new Array(value) when you mean one element.
Sources
related.
Ruslan Osipov
About the author