How to initialize an array's length in JavaScript?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In JavaScript, you can set an array's length when you create it, but that does not always create actual elements. The important distinction is between an array with empty slots and an array filled with usable values such as 0, null, or objects.
new Array(length) Creates Empty Slots
The most direct way to allocate an array of a specific length is:
This prints a length of 5, but the array contains empty slots, not normal values. That distinction matters because many array methods behave differently with empty slots than with actual undefined entries.
For example:
The callbacks do not run over missing elements the same way they run over real values.
Use fill When You Want Actual Values
If your real goal is “make an array of length n containing a starting value,” combine the constructor with fill.
That creates real elements, which makes methods like map, forEach, and reduce behave normally.
This is often the best answer when the array is meant to hold primitive placeholders.
Be Careful with Objects in fill
fill reuses the same object reference for every slot when you pass an object.
All three entries point to the same array, so changing one changes all of them. If you need distinct objects, use Array.from instead.
This creates a new array for each position.
Use Array.from for Initialization Logic
Array.from is one of the cleanest tools when you want both a length and per-index initialization.
This produces [0, 1, 2, 3, 4].
It is a better choice than creating empty slots and then trying to map over them, because the elements are created intentionally during construction.
Setting length Later
You can also resize an existing array by changing its length property.
Increasing the length adds empty slots. Decreasing the length truncates the array.
This is valid, but it is usually clearer to create the array in the intended shape upfront unless you are deliberately trimming or expanding it.
Which Pattern to Choose
A practical rule is:
- use
new Array(n)when you only care about reserved length or will assign elements manually later - use
new Array(n).fill(value)for a repeated primitive default - use
Array.from({ length: n }, fn)when each element needs unique initialization - avoid
fillwith objects unless shared references are exactly what you want
This is less about syntax preference and more about the semantics of the resulting array.
Performance and Readability
For most application code, readability matters more than micro-optimizing how the array was initialized. Modern JavaScript engines are very good at array operations, but sparse arrays and dense arrays are treated differently internally.
If the array is meant for normal iteration and data processing, dense arrays with real elements are usually the safer and clearer choice.
A sparse array created accidentally can lead to surprising bugs, especially when methods skip missing elements silently.
Common Pitfalls
A common mistake is thinking new Array(5) creates five undefined values. It creates five empty slots.
Another mistake is using fill([]) or fill({}) and expecting each slot to get a fresh object. All entries share the same reference.
Developers also sometimes try to use map directly on a newly constructed sparse array and then wonder why nothing happens.
Finally, resizing an array by setting length can silently drop data when the new length is smaller. That is correct behavior, but it should be done intentionally.
Summary
- '
new Array(n)sets the length but creates empty slots.' - '
fillcreates actual values and is usually better for usable initialized arrays.' - '
Array.fromis ideal when each position needs custom initialization.' - Do not use
fillwith objects unless shared references are intended. - Choose dense arrays over sparse ones for most everyday JavaScript code.
Related reading
- How to initialize HashSet values by construction?
- How to initialize List<String> object in Java?
- How to input a list of lists with different sizes in tf.data.Dataset
- How to interpret a Java thread stack?
- How to inject Javascript in WebBrowser control?
- How to insert an element after another element in JavaScript without using a library?
- How to intersect two sorted integer arrays without duplicates?
- How to invert a permutation array in numpy

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.