Create Two Dimensional Array in JavaScript

Create Two Dimensional Array in JavaScript


In this example, you will learn to write a JavaScript program that will create a two dimensional array.

To understand this example, you should have the knowledge of the following JavaScript programming topics:


JavaScript for loop

JavaScript Array

JavaScript Function and Function Expressions


Example: Two Dimensional Array Using for Loop



In the above program, the first argument of the twoDimensionArray() function represents the number of array elements,

and the second argument represents the number of array elements inside of each array element.

The first for loop is used to create a two dimensional array. [[], []]

The second for loop iterates over each array element and inserts the elements inside of an array element.

When i = 0, the elements are inserted to the first array element [[0, 1, 2], []].

When i = 1, the elements are inserted to the second array element [[0, 1, 2], [0, 1, 2]].

Post a Comment

Previous Post Next Post