What is the most efficient way to create a zero filled JavaScript array of arbitrary length ?
-
1Is there any reason you want zero-filled as opposed to an array of X length filled with `undefined` which can easily be declared as `new Array(x)` where `x` is integer between 0 and 2^32 - 1. – Mike Brant Nov 26 '13 at 16:30
8 Answers
By default Uint8Array
, Uint16Array
and Uint32Array
classes keep zeros as it's values, so you don't need any complex filling techniques, just:
var ary = new Uint8Array(10);
all elements of array ary
will be zeros by default.

- 9,126
- 7
- 34
- 61

- 13,861
- 4
- 29
- 29
-
Perfect. This should be at the top. Stackoverflow pick the best answers not based on the upvote. @deadrunk – Nandakumar Feb 26 '20 at 07:53
In Javascript ES6 there is a very easy solution. I came here because I'm trying to codegolf it shorter:
n = 999 // arbitrary length
a = Array(n).fill(0)
console.log(a)

- 8,075
- 5
- 46
- 66
New ES6 array extensions allow you to do this natively with fill
method. Now IE edge, Chrome and FF supports it, but check the compatibility table
new Array(3).fill(0)
will give you [0, 0, 0]
. You can fill the array with any value like new Array(5).fill('abc')
(even objects and other arrays).
On top of that you can modify previous arrays with fill:
arr = [1, 2, 3, 4, 5, 6]
arr.fill(9, 3, 5) # what to fill, start, end
which gives you: [1, 2, 3, 9, 9, 6]

- 214,103
- 147
- 703
- 753
How about trying like this:
Array.apply(null, new Array(10)).map(Number.prototype.valueOf,0);
//Output as [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
or
new Array(10+1).join('0').split('').map(parseFloat)
//Output as [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
EDIT:-
If your array is dynamic then simply put that in a function which takes a number and replace 10 by that variable.

- 168,305
- 31
- 280
- 331
-
-
1just put that in a function that take a number and replace 10 by that variable! – Shryme Nov 26 '13 at 16:46
-
-
1The OP asked for the most efficient way, which I think is @kennebec's answer: http://jsperf.com/fill-array-stackoverflow. I've just tested the different methods in Chrome so far. – jameslafferty Nov 26 '13 at 20:06
-
-
@mbomb007 I just didn't add one at the time... Just tried to, but jsperf.com doesn't seem to be letting me update it :/ – jameslafferty Jan 12 '18 at 21:28
-
@RahulTripathi can you explain how your first solutions works? I'm interested in this part: `map(Number.prototype.valueOf,0);` – CuteShaun Sep 30 '20 at 14:06
If you want a 9-length array:
Array.apply(null, {length: 9}).map(function() {return 0;})
If you want a X-length array:
Array.apply(null, {length: X}).map(function() {return 0;})
If you have an array and want to rewrite its values:
var arr=[54,53,6,7,88,76]
arr=arr.map(function() {return 0;})
You can fill the array with anything you want, just by changing the value at return inside the function inside .map:
Array.apply(null, {length: 9}).map(function() {return "a string";})
Array.apply(null, {length: 9}).map(function() {return Math.random();})
Array.apply(null, {length: 9}).map(function() {return NaN;})
Array.apply(null, {length: 9}).map(function() {return null;})
Array.apply(null, {length: 9}).map(function() {return true;})
Array.apply(null, {length: 9}).map(function() {return;})
The last one will fill the array with undefined

- 197
- 2
- 9
function repeatArray(value, len){
var A=[],i=0;
while (i<len) A[i++]= value;
return A;
}

- 102,654
- 32
- 106
- 127
I tested both the Unit8Array
[].slice.apply(new Uint8Array(n));
and the join/split method
new Array(n + 1).join('0').split('').map(parseFloat);
in jsPerf with interesting results. Chrome and Firefox were fairly slow on both counts (between 48,000 and 76,000 operations). But Opera and Safari both did amazingly well with the Uint8Array function (up to 438,000 operations). I think I'll start using Uint8Array and hope Chrome and Firefox improve.

- 3,627
- 8
- 33
- 46
One way could use recursion.
function fillArray(l, a) {
a = a || [];
if (0 < l) {
l -= 1;
a[l] = 0;
return fillArray(l, a);
}
return a;
}
I had also posted a more classic option:
function fillArray(l) {
var a;
for (a = []; 0 < l; l-=1, a[l]=0);
return a;
}

- 2,152
- 2
- 21
- 25