This post will discuss how to create a two-dimensional array in JavaScript.

JavaScript offers several ways to create a two-dimensional array of fixed dimensions:

1. Using Array constructor

Using the array constructor and the for-loop, creating a two-dimensional array in JavaScript is as simple as:

Download  Run Code

2. Using array literal notation

2D Arrays can be created using the literal notation, as shown below:

Download  Run Code

3. Using Array.from() function

The Array.from() method creates a new Array instance from the specified array and optionally map each array element to a new value. To create a 2D array, the idea is to map each element of the length of M to a new empty array of length N.

Download  Run Code

4. Using Array.prototype.map() function

Alternatively, you can directly call the map() function on the array, as shown below:

Download  Run Code

That’s all about creating a two-dimensional array in JavaScript.