![]() |
2d Array |
Two-dimensional arrays are created the same way single dimensional arrays are. And you access them like array[0][1]. To create a 2D array in JavaScript we can create an Array first and then addArrays as it's elements. This method will return a 2D array with the given number of rows and columns.
2D Array in JavaScript:
<html>
</script>
</script>
<head>
<title>2d array</title>
<script type="text/javascript">
function f1()
{
var x=[];
for(var i=0;i<3;i++)
{
var y=[];
for(var j=0;j<3;j++)
{
var n=prompt("Enter Element"+i);
y.push(n);
}
x.push(y);
}
for(var i=0;i<3;i++){
for(var j=0;j<3;j++)
{
document.getElementById("d1").innerHTML+=(x[i][j]+" ");
}
document.getElementById("d1").innerHTML+="<br>";
}
}
<style type="text/css">
#d1
{
position:absolute;
width:300px;
top:50px;
left:50px;
height:300px;
border:1px solid black;
background-color:green;
}
</style>
<body onload="f1()">
<div id="d1">
</div>
</body>
</html>
Output:
Enter Element0 value Enter Three Time.
Enter Element1 value Enter Three Time.
Enter Element0 value Enter Three Time.
More Related Program:
0 Comments