+ 8

How to declare an empty multidimensional array in JavaScript?

My JavaScript code needs looping to declare a multidimensional array. Is there a best way to replace it? My magic square: https://code.sololearn.com/WBwWcTsdjxcd/?ref=app

29th Sep 2017, 10:33 AM
Ricky Afdita A P
Ricky Afdita A P - avatar
11 Answers
+ 6
Javascript doesn't need formal declaration like C# or other strongly typed languages. Multidimensional arrays are just nested arrays. My first piece of advice is to do yourself a favor and use descriptive variable names. That code is really hard to read, and while you're in it, it might make sense; six months from now you'll go back and just be lost. I've created a code to hopefully better explain multidimensional arrays in javascript. There's also an example pattern you can experiment with in the JS panel. https://code.sololearn.com/Wn75Hc7jTOZw#js
29th Sep 2017, 6:00 PM
St John Peaster
St John Peaster - avatar
+ 3
up vote 0 down vote A nested 3x3 array (of undefined): var arr = new Array(3); for (var i = 0; i < arr.length; ++i) { arr[i] = new Array(3); } console.log(arr);
2nd Oct 2017, 12:39 PM
Shaikh Mohammad Zahid Mohammad Salim
Shaikh Mohammad Zahid Mohammad Salim - avatar
+ 2
Reminder, this is a JavaScript discussion, not Java.
2nd Oct 2017, 12:18 PM
St John Peaster
St John Peaster - avatar
+ 1
let width=5; let height=5; let emptyArray = [...Array(height)].map(e => Array(width).fill(0)); alert(emptyArray );
27th Jul 2020, 2:37 PM
mahammedi abdelghani
mahammedi abdelghani - avatar
- 1
Maulik, I don't think it works like that in javascript 😅
29th Sep 2017, 3:50 PM
Lord Teacrumble
Lord Teacrumble - avatar
- 1
how do u code this like what should I get in place first
30th Sep 2017, 8:53 AM
Android Boi
Android Boi - avatar
- 1
To declare a Two Dimensional Array in Java, you can use either data_type[][] array_name = new data_type[SIZE][SIZE] ; (or) data_type array_name[][] = new data_type[SIZE][SIZE] ;
2nd Oct 2017, 11:46 AM
Naveen Bharathi
Naveen Bharathi  - avatar
- 2
TO DECLARE TWO DIMANTION ARRAY IN JAVA I.E TWO DIMANTION ARRAY DATA TYPE[SIZE][SIZE]=new DATA TYPE
29th Sep 2017, 3:48 PM
Maulik Solanki
Maulik Solanki - avatar
- 2
i told in java not java script..
29th Sep 2017, 3:52 PM
Maulik Solanki
Maulik Solanki - avatar
- 2
it doesn't happen in JavaScript
30th Sep 2017, 10:11 PM
mfonjohn
mfonjohn - avatar
- 3
do you know how big the multidimensional array will be?
29th Sep 2017, 11:18 AM
Lord Teacrumble
Lord Teacrumble - avatar