# How to create a 2d array of zeroes in javascript?

Is there an easy way to programmatically create a 2d array in javascript?

What I don’t want:

``````var array2D = [
[0,0,0],
[0,0,0],
[0,0,0]
]
``````

# Solution 2017:

Late to the Party, but this Post is still high up in the Google search results.

### To create an empty 2D-Array with given size (adaptable for more dimensions):

``````let array = Array(rows).fill().map(() => Array(columns));
``````

### Prefilled 2D-Array:

``````let array = Array(rows).fill().map(() => Array(columns).fill(0));
``````

E.g.:

``````Array(2).fill().map(() => Array(3).fill(42));
// Result:
// [[42, 42, 42],
//  [42, 42, 42]]
``````

### Warning:

`Array(rows).fill(Array(columns))` will result in all rows being the reference to the same array!!

## Update 24th September 2018 (thanks to @Tyler):

Another possible approach is to use `Array.fill()` to apply the map function.

E.g.:

``````Array.from(Array(2), _ => Array(3).fill(43));
// Result:
// [[43, 43, 43],
//  [43, 43, 43]]
``````

### Benchmark:

https://jsperf.com/multi-dimensional-array-map-vs-fill/5

Well, you could write a helper function:

``````function zeros(dimensions) {
var array = [];

for (var i = 0; i < dimensions; ++i) {
array.push(dimensions.length == 1 ? 0 : zeros(dimensions.slice(1)));
}

return array;
}

> zeros([5, 3]);
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
``````

Bonus: handles any number of dimensions.

``````function zero2D(rows, cols) {
var array = [], row = [];
while (cols--) row.push(0);
while (rows--) array.push(row.slice());
return array;
}
``````

You can use the following function to create a 2D array of zeros:

``````const zeros = (m, n) => [...Array(m)].map(e => Array(n).fill(0));

console.log(zeros(3, 4));

// [ [ 0, 0, 0, 0 ],
//   [ 0, 0, 0, 0 ],
//   [ 0, 0, 0, 0 ] ]``````

A no-frills, easy-to-understand method for beginners:

``````function twoDarrMaker(row, col) {
const twoDarr = [];

for (let i = 0; i < row; i++) {
let subarray = [];

for (let j = 0; j < col; j++) {
subarray.push(0);
}

twoDarr.push(subarray);
}

return twoDarr;
}

twoDarrMaker(3, 2);
// [
//   [ 0, 0 ],
//   [ 0, 0 ],
//   [ 0, 0 ]
// ]
``````

Edit: Here’s an explanation of my code. You create an array, `twoDArr`, to hold the 2d array, or array of arrays. You use the outer for loop to create subarrays. Then you use the inner for loop to add zeros to the subarray. Then you add the subarray to the outer array, `twoDarr`. Then you repeat the process however many times the outer for loop specifies. When the outer for loop is finished, you return the 2d array.

Read More:   Remove all elements from array that match specific string

Basically, the outer for loop creates the rows (subarrays), while the inner for loop creates the columns (elements in the subarrays). The outer for loop tells you to create `x` rows (subarrays) by repeating the code in the block `x` times, and the inner for loop tells you to create `y` columns (elements) by repeating the code in the block `y` times. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .