Practically? Yes. You can create an array of arrays which functions as an 2D array as every item is an array itself:

let items = [
  [1, 2],
  [3, 4],
  [5, 6]
];
console.log(items[0][0]); // 1
console.log(items[0][1]); // 2
console.log(items[1][0]); // 3
console.log(items[1][1]); // 4
console.log(items);

But technically this is just an array of arrays and not a “true” 2D array, as I. J. Kennedy pointed out.

It should be noted that you could keep nesting arrays into one another and so create “multidimensional” arrays.

Answer from Ballsacian1 on Stack Overflow
🌐
Math.js
mathjs.org › docs › datatypes › matrices.html
math.js | an extensive math library for JavaScript and Node.js
A Matrix is an object wrapped around a regular JavaScript Array, providing utility functions for easy matrix manipulation such as subset, size, resize, clone, and more.
Discussions

Column of a matrix [array]
I solved this Freecodecamp challenge, pushing the largest(row) number in each array into the newArr · How can i make the code be printed in a column and get this output [1000, 1001, 857, 39] · Why are you hard coding 0? You needed needed a nested loop to go through the rows, so you will similarly ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
August 13, 2022
javascript - Convert simple array into two-dimensional array (matrix) - Stack Overflow
1 How can I convert 1 dimensional array into 2 dimensional array based on certain key in javascript? More on stackoverflow.com
🌐 stackoverflow.com
How do you easily create empty matrices javascript? - Stack Overflow
This is an exact fix to your problem, but I would advise against initializing the matrix with a default value that represents '0' or 'undefined', as Arrays in javascript are just regular objects, so you wind up wasting effort. More on stackoverflow.com
🌐 stackoverflow.com
Loop 2d (or more) array by column instead of row first?
But I am really wrong??? Wrong about what? I don't understand exactly what your disagreement with your teacher is. A teacher (python) tells that we can loop through 2d matrix by rows or by columns (in algo or python) Are you trying to say you disagree that it's possible to loop through a 2D array first by columns and then by rows? It's definitely possible. Just loop over the column indices in the outer loop, and the row indices in the inner loop. Also, they said array[j][i]==array[i][j] in their explanation. Well, obviously that's not true in general. If array[i][j] is a valid array element, then array[j][i] might not even be inside the bounds of the array (if it's not a square array). I give example of stackoverflow link https://stackoverflow.com/questions/10148818/numpy-how-to-iterate-over-columns-of-array , but they said it is because it is Numpy. Yes, most of the answers in that thread are specific to Numpy arrays and will not work on a standard Python list-of-lists. I try their solution and it fails. Teacher is sure it will work even if i try to show the issue. What solution is this? Without seeing the code, I don't see how anyone can tell you if it's correct or not. More on reddit.com
🌐 r/learnprogramming
20
0
January 18, 2024
🌐
freeCodeCamp
freecodecamp.org › news › javascript-2d-arrays
JavaScript 2D Array – Two Dimensional Arrays in JS
November 7, 2024 - In JavaScript, we use two-dimensional arrays, also known as matrices, to store and manipulate data in a structured and organized manner. They allow for the efficient storage and manipulation of large amounts of data, such as in image or video ...
🌐
Medium
rikyperdana.medium.com › multi-dimensional-matrix-in-functional-js-b2628df76d5e
Multi Dimensional Matrix in Functional JS | by Riky Perdana | Medium
April 9, 2024 - Another alteration to the function earned us the function above, which is not only capable of generating a multi-dimensional matrix with n-depth but also have a callback (cb) that returns its position in array form.
🌐
npm
npmjs.com › package › javascript-array-matrix
javascript-array-matrix - npm
A lightweight and performant data structure for storing JavaScript objects in an n-way or n-order array matrix.
      » npm install javascript-array-matrix
    
Published   Jun 07, 2018
Version   0.0.7
Author   Tim Steele
🌐
GitHub
github.com › express-labs › javascript-array-matrix
GitHub - express-labs/javascript-array-matrix: A JavaScript object for storing and querying multi-dimensional or n-way array data
A lightweight and performant data structure for storing JavaScript objects in an n-way or n-order array matrix.
Author   express-labs
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Column of a matrix [array] - JavaScript - The freeCodeCamp Forum
August 13, 2022 - function largestOfFour(arr) { let newArr = []; for(let i = 0; i max){ max = arr[i][j] } } newArr.push(max) } return newArr } console.log(largestOfFour([ [4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], ...
Find elsewhere
🌐
Socialhackersacademy
socialhackersacademy.org › home › matrices in javascript: multi-dimensional arrays.
Matrices in JavaScript: Multi-Dimensional Arrays. -
August 20, 2021 - If we look closely we can see that ... = [g,h,i] ... To put it simply, we can create a matrix by having a one-dimensional Array, where each entry is another one-dimensional Array!...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-create-two-dimensional-array-in-javascript
How to Create Two Dimensional Array in JavaScript? - GeeksforGeeks
November 8, 2024 - It is a data structure in JavaScript that can hold values in rows and columns form. It is an array of arrays. Each element in a 2D array is accessible using two indices, and it is represented as ...
🌐
W3docs
w3docs.com › javascript
How to Create a Two Dimensional Array in JavaScript
Multidimensional arrays are known ... array. They can have more than two dimensions. A two-dimensional array is also called a matrix or a table of rows and columns....
🌐
DEV Community
dev.to › yanhaijing › mastering-javascript-multiple-ways-to-generate-a-two-dimensional-array-cpg
Mastering JavaScript: Multiple Ways to Generate a Two-Dimensional Array - DEV Community
April 13, 2024 - This version of create2DArray uses Array.from to create a new array for each row, ensuring that each row is independent of the others. Now modifying one row won't affect the others. ... This shares a striking resemblance to Method 2 in its ingenuity. ... Thank you for your feedback, this is a better approach. ... I prefer try it in a way closer to OO paradigme, creating a function that will represent a matrix in general this give to us a more reusefull project feature.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › WebGL_API › Matrix_math_for_the_web
Matrix math for the web - Web APIs - MDN Web Docs
2 weeks ago - There are many types of matrices, but the ones we are interested in are the 3D transformation matrices. These matrices consist of a set of 16 values arranged in a 4×4 grid. In JavaScript, it is easy to represent a matrix as an array.
🌐
Medium
giuliacajati.medium.com › mastering-matrices-in-javascript-a-guide-to-multidimensional-arrays-part-1-219f0e71607c
Mastering Matrices in Javascript: A guide to Multidimensional Arrays (Part 1) | by Giulia Elizabeth | Medium
February 22, 2021 - Now every-time we iterate through we will move both diagonals towards the bottom (left or right) of the array, and add the value to the sum. This will stop once i = 4 and is no longer < arr.length · This can also be done with a forEach loop as you can see below. ... The next problem we will be looking at is Leetcode 1380. Here we are asked to return the number that is both the minimum in its row and maximum in its column. ... Iterate though the matrix.
🌐
Sentry
sentry.io › sentry answers › javascript › how can i create a two-dimensional array in javascript?
How can I create a two-dimensional array in JavaScript? | Sentry
February 15, 2023 - There are, broadly speaking, two ways of creating a 2D array in JavaScript. You can create an array using array literal notation or you can create a 1D array and then loop through each item in the array to create nested arrays.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-create-and-access-matrix
JavaScript - Create and Access Matrix - GeeksforGeeks
July 23, 2025 - Here, the matrix is represented as a 2D array, and you can access an element by specifying the row and column indices.
Top answer
1 of 16
122

Array.fill

Consider using fill:

Array(9).fill().map(()=>Array(9).fill())

The idea here is that fill() will fill out the items with undefined, which is enough to get map to work on them.

You could also fill directly:

Array(9).fill(Array(9))

(important note: Array(9).fill(Array(9)) will fill each row of the array with the same array, so changing one row will change the other rows).

Alternatives to Array(9).fill() include

Array(...Array(9))
[].push(...Array(9))
[].concat(Array(9))
Array.from(Array(9))

We can rewrite the solution a bit more semantically as:

function array9() { return Array(9).fill(); }
array9().map(array9)

or

function array(n) { return Array(n).fill(); }
array(9).map(() => array(9))

Array.from provides us with an optional second mapping argument, so we have the alternative of writing

Array.from(Array(9), () => Array.from(Array(9));

or, if you prefer

function array9(map) { return Array.from(Array(9), map); }
array9(array9);

For verbose description and examples, see Mozilla's Docs on Array.prototype.fill() here.
and for Array.from(), here.

Note that neither Array.prototype.fill() nor Array.from() has support in Internet Explorer. A polyfill for IE is available at the above MDN links.

Partitioning

partition(Array(81), 9)

if you have a partition utility handy. Here's a quick recursive one:

function partition(a, n) {
  return a.length ? [a.splice(0, n)].concat(partition(a, n)) : [];
}  

Looping

We can loop a bit more efficiently with

var a = [], b;
while (a.push(b = []) < 9) while (b.push(null) < 9);

Taking advantage of the fact that push returns the new array length.

2 of 16
95
var matrix = [];
for(var i=0; i<9; i++) {
    matrix[i] = new Array(9);
}

... or:

var matrix = [];
for(var i=0; i<9; i++) {
    matrix[i] = [];
    for(var j=0; j<9; j++) {
        matrix[i][j] = undefined;
    }
}
🌐
Medium
dougschallmoser.medium.com › javascript-matrix-creation-3222c5113478
JavaScript Matrix Creation. I was recently working on a coding… | by Doug Schallmoser | Medium
February 24, 2021 - The Matrix is an oldie but goodie. Ultimately, what we need to create is an array of arrays, and then fill in each subarray with objects. Two arguments are provided when creating the grid: 1) rows — number of rows (each row is an array) 2) cols — number of columns (each column is composed of objects) ... So we will build this with a JavaScript class called Matrix and initiate the grid inside the constructor function.
🌐
Medium
medium.com › @ryan_forrester_ › two-dimensional-arrays-in-javascript-how-to-guide-8faa070abab5
Two-Dimensional Arrays in JavaScript (How to Guide) | by ryan | Medium
September 12, 2024 - A two-dimensional array in JavaScript can be created by nesting arrays within an outer array. const matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ];
🌐
Scaler
scaler.com › home › topics › javascript 2d array
2D Arrays in JavaScript - Scaler Topics
January 1, 2024 - The javascript 2D array is a collection of the same type of elements that can be stored as a matrix using the various rows and columns. Each of the elements of the array is stored in these rows and columns using some specific index value.