Two Dimensional Arrays in JavaScript

JavaScript gives you One-Dimension Array creation feature, But if we want to perform 2d Array calculation then we will have to create the 2d array then JavaScript does not have any ready made function for 2D Array but we can create 2D Array through JavaScript programming. This tutorial will guide you through creating an 2d array in JavaScript. The technique behind creating an 2d array is to create another one-dimension array inside an existing array element.




<script type="text/javascript">
	var data = new Array();	//Create an new 1D array
 
	for(i=0;i&lt;100;i++)
	{
		data[i] = new Array();	//Create another array inside data[] array
		data[i][0] = i;		//Adding data to the data array
		data[i][1] = i+1;	//Adding data to the data array
	}
 
	//Output the array content
	document.write(data[0][0]);
	document.write(data[0][1]);
	document.write(data[1][0]);
	document.write(data[1][1]);
</script>

Output:
0 1 1 2

Explanation:

Similar Posts

Custom Search

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)