Stack Overflow
stackoverflow.com › questions › 966225 › how-can-i-create-a-two-dimensional-array-in-javascript
How can I create a two dimensional array in JavaScript? - Stack Overflow
I have been reading online and some places say it isn't possible, some say it is and then give an example and others refute the example, etc. How do I declare a 2 dimensional array in JavaScript? (
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
July 28, 2026 - The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations.
Sentry
sentry.io › sentry answers › javascript › how can i create a two-dimensional array in javascript?
JavaScript Two-Dimensional Array: Create a Matrix | Sentry
4 weeks ago - It takes in an iterable or array-like object to convert to an array as its first argument. The optional second argument, mapFn, is a function that’s called on every item of the array. Its return value is added to the array. In the example code below, each new row created for the 2D array is an array with a length of 5.
CodeSignal
codesignal.com › learn › courses › multidimensional-arrays-and-their-traversal-in-javascript › lessons › multidimensional-arrays-and-their-traversal-in-javascript
Multidimensional Arrays and Their Traversal in JavaScript
JavaScript offers various ways to manage multidimensional arrays. For instance, you can determine the number of rows (floors) and columns (units on each floor): ... let array = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; // Finding the number of rows let numFloors = array.length; console.log(numFloors); // Outputs: 3 // Finding the number of columns let numUnits = array[0].length; console.log(numUnits); // Outputs: 3 · To add a new row to a 2D static array, you typically add the new row at the end of the array, as dynamically inserting rows at specific positions can be complex and is not generally recommended for static arrays.
Scaler
scaler.com › home › topics › what is a multidimensional array in javascript?
Multidimensional Array in JavaScript - Scaler Topics
July 14, 2024 - A multidimensional array is not present natively in JavaScript. Hence there is no direct way to create a multidimensional array in JavaScript.
Reddit
reddit.com › r/learnjavascript › how to create a two-dimensional array?
r/learnjavascript on Reddit: How to create a two-dimensional array?
July 30, 2022 -
Just started using javascript and I need a 2D-Array (coming from C++ where this is pretty normal) but I am not sure how to declare such an array and initialise it with zeros.
Thank you
Edit: After searching a bit I came across this(if anyone could explain me how it does it I would really appreciate it - I can use it but its better to fully understand it)
let Table = (r,c) => [...Array(r)].map(x=>Array(c).fill(0));
Top answer 1 of 2
2
There is another method on the Array object that I would use, but let's talk about the spread and fill method you have here. [...Array(r)] We call the Array constructor with the argument r, creating an array of r empty slots. Then we spread that array , creating another array of r elements each having the value undefined. This allows us to act on each element. It has the same effect as this: Array(r).fill('blah') We are going to replace each element in the next step anyway. .map(x => /* ... */) Here we .map() each value to some other value. Array(c).fill(0) That other value is itself an array of size c, which we .fill() with zeroes . The result is an array of r arrays of c zeroes. Now here is how I would do it: Array.from() takes as its first argument an array-like object and as its second argument a mapping function. So we can do this: const Table = (r, c) => Array.from( { length: r }, () => Array(c).fill(0) ) Now we are creating and mapping the outer array in one swoop instead of creating, then spreading, then mapping.
2 of 2
1
One way would be as an array of arrays; I guess that is the popular technique. Another way would be to combine the indices as a single string, like `${i}:${j}` and use that to index an object.
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-multidimensional-array
JavaScript Multidimensional Array - GeeksforGeeks
There are two common ways to create a multidimensional array in JavaScript: The easiest and most common way to create a multidimensional array is by using square brackets ([]), also known as array literals.
Published: July 12, 2025
freeCodeCamp
forum.freecodecamp.org › curriculum help
Creating 2D array in javascript - Curriculum Help - The freeCodeCamp Forum
January 9, 2023 - Hi All, I have a below code in which I am trying to fill the data from table that has few rows and columns. The problem is that I am unable to get the right value at indexes outside the loop. I need to add values at each row of the array and return the array to use in other functions.
Naukri
naukri.com › code360 › library › how-to-create-two-dimensional-array-in-javascript
How to create Two Dimensional Array in JavaScript?
April 3, 2024 - Almost there... just a few more seconds