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

Your email:

 


Explanation:

  • Here i am creating another array inside the individual array element
  • This gives me 2 data storage for every individual element i.e [0][0]-[0][1], [1][0]-[1][1] etc
  • Finally i am adding data to the array element

Custom Search


Popular Articles:

Categories: Javascript Tags:
  1. No comments yet.
  1. No trackbacks yet.