The Array.from() method creates a new, shallow-copied Array instance from an array-like or iterable object.

function createAndFillTwoDArray({
  rows,
  columns,
  defaultValue
}){
  return Array.from({ length:rows }, () => (
      Array.from({ length:columns }, ()=> defaultValue)
   ))
}

console.log(createAndFillTwoDArray({rows:3, columns:9, defaultValue: 'q'}))

Answer from Mohammed Ashfaq on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-2d-array
JavaScript 2D Array - GeeksforGeeks
July 11, 2025 - In this approach, we will use the fill() method and map method for creating the two-dimensional array in JavaScript. Example: This example shows the implementation of above-explained approach.
Discussions

Filling up a 2D array with random numbers in javascript - Stack Overflow
I'm really sorry if anything like this has been posted here before but I couldn't find anything, I'm kinda new to the site still! So for a while now I've been learning a bit about game development More on stackoverflow.com
๐ŸŒ stackoverflow.com
How can I create a two dimensional array in JavaScript? - Stack Overflow
Assuming a somewhat pedantic definition, it is technically impossible to create a 2d array in javascript. But you can create an array of arrays, which is tantamount to the same. I. J. Kennedy โ€“ I. J. Kennedy ยท 2014-07-29 05:05:59 +00:00 Commented Jul 29, 2014 at 5:05 ... FYI... when you fill an ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to create a two-dimensional array?
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. More on reddit.com
๐ŸŒ r/learnjavascript
2
2
July 30, 2022
Why do 2d arrays behave differently when I create them using Array.fill() vs when I create them by iterating over the rows and columns?
because you're filling with the same array, not a new array each time here's another way to see it const base = new Array(5), el = []; base.fill(el); el[0] = 'hello'; console.log(base); The issue here is that you're filling the outer array with pointers to the same inner array over and over More on reddit.com
๐ŸŒ r/learnjavascript
10
15
March 26, 2022
๐ŸŒ
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.
๐ŸŒ
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 - You can also create a 2D array by creating rows using the Array() constructor and then creating the nested arrays using the Array.map() method: const rows = 4; const columns = 5; let my2DArray = Array(rows) .fill() .map(() => Array(columns)...
๐ŸŒ
Zipy
zipy.ai โ€บ blog โ€บ how-can-i-create-a-two-dimensional-array-in-javascript
how can i create a two dimensional array in javascript
April 12, 2024 - The fill() method is used to initialize array elements to a specific value, in this case, another array representing a row. When both the dimensions and the initial values of the array are variable, using loops to dynamically construct a 2D ...
๐ŸŒ
Sanori's Blog
sanori.github.io โ€บ 2019 โ€บ 05 โ€บ JavaScript-Pitfalls-Tips-2D-Array-Matrix
JavaScript Pitfalls & Tips: 2D Array, Matrix | Sanori's Blog
August 5, 2019 - In JavaScript, an array of arrays can be used as a 2D array. How do you implement if you initialize m x n matrix with zeros? If you know about Array.prototype.fill(), you may be tempted to initialize m x n array like Array(m).fill(Array(n).fill(0)), but this does not work properly.
Find elsewhere
๐ŸŒ
30 Seconds of Code
30secondsofcode.org โ€บ home โ€บ javascript โ€บ array โ€บ initialize 2d array
How can I initialize 2D array in JavaScript? - 30 seconds of code
December 27, 2023 - 2D arrays, also known as matrices, are pretty common in many areas of programming. While some languages provide a built-in way to initialize a 2D array, JavaScript does not. Given a width and height, you can create a 2D array and fill it with a specific value.
๐ŸŒ
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));

๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ why do 2d arrays behave differently when i create them using array.fill() vs when i create them by iterating over the rows and columns?
r/learnjavascript on Reddit: Why do 2d arrays behave differently when I create them using Array.fill() vs when I create them by iterating over the rows and columns?
March 26, 2022 -
const rows = 4;
const cols = 5;
const visited1 = new Array(rows); // create an empty array of length n
for (let i = 0; i < rows; i++) {
  visited1[i] = new Array(cols); // make each element an array
}
for(let i = 0;i<rows;i++){
	for(let j = 0;j<cols;j++)
		visited1[i][j]=false;
}
const visited2 = new Array(rows).fill((new Array(cols)).fill(false))
console.log('visited1 is \n',visited1)

console.log('visited2 is \n',visited2)
visited1[1][1]=true;
visited2[1][1]=true;

console.log('after changes, visited 1 is \n',visited1)
console.log('after changes, visited 2 is \n',visited2)

Why are the 2 arrays visited1 and visited2 behaving differently when i alter the value at an index?

Spent around an hour debugging a code where I was trying to implement a breadth first search. I created a visited matrix to keep track of all vertices I have already visited using the Array.fill() method , but when i modify visited[i][j], it changes the entire column instead of just the value at that index. Why is this happening?

๐ŸŒ
KIRUPA
kirupa.com โ€บ javascript โ€บ 2d_arrays.htm
Two-Dimensional (2D) Arrays in JavaScript
Here we have a create2dArray function that creates a 2D array based on the number of rows and columns we specify. We also specify the default value each cell will store. The end result of our code here is a 3x4 array with each cell having a ...
๐ŸŒ
Envato Tuts+
code.tutsplus.com โ€บ home โ€บ coding fundamentals
Working With 2D Arrays in JavaScript | Envato Tuts+
February 19, 2023 - The second argument is the number of columns, which dictates how many elements there should be within each array. Finally, we have the third argument, which is the value that we want to set for each element in our 2D array. Inside the function, we create a for loop that iterates over all the rows and fills the parent array element with new arrays.
๐ŸŒ
W3docs
w3docs.com โ€บ javascript
How to Create a Two Dimensional Array in JavaScript | W3Docs
Example of iterating over the elements in a two-dimensional array in JavaScript ... Output appears here after Run. ... Output appears here after Run. In this example, we've used a loop to initialize each row as a separate array. โš ๏ธ Important: When creating 2D arrays, avoid using Array.fill() with an existing array, as it copies the reference rather than creating new instances.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ so i wanted to find a way to fill a 2d array with non referenced objects, and came to this solution.
r/learnprogramming on Reddit: So I wanted to find a way to fill a 2D array with non referenced objects, and came to this solution.
February 12, 2022 -

I'm making a wordle copy in react, and wanted to initialize all the boxes in one go, after an hour or 2 of debugging and wondering why when I entered a letter, all the boxes would fill up with the letter, I realized it was because a made an array of references to the same object.

So after another hour I came to this solution:

Array(6).fill(null).map(() =>Array(5).fill(null).map(() => ({ ...box })))

Another view:

Array(6)
.fill(null)
.map(() =>
Array(5)
.fill(null)
.map(() => ({ ...box }))
)

I was wondering how god awful this is. And what a better solution would be.

Should I scrap the array of objects and just have an array of letters? This was v1 of the project but, playing with the position and indexes of the letters and words was a nightmare.

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 61463386 โ€บ javascript-use-array-fill-function-to-init-a-2d-array
javascript use Array fill() function to init a 2D array - Stack Overflow
April 27, 2020 - when I init use fill to init a 2D array like this let dp = new Array(n); dp.fill(new Array(n).fill(false)); if I change the dp[0][0] it will be [ [ true, false, false, false, false ], [
๐ŸŒ
DEV Community
dev.to โ€บ iraycd โ€บ how-to-create-a-2d-array-on-integers-in-js-14md
How to create a 2D array of integers in JS? - DEV Community
September 20, 2022 - Fill is a prototype of Array. The fill() method changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length).