How to create a two dimensional array in JavaScript?


A two-dimensional array has more than one dimension, such as myarray[0][0] for element one, myarray[0][1] for element two, etc. To create a two-dimensional array in JavaScript, you can try to run the following code −

Example

Live Demo

<html>    
   <body>              
      <script>  
         var myarray=new Array(3);        
         for (i=0; i <3; i++)          
         myarray[i]=new Array(3)                
         myarray[0][0]="One"        
         myarray[0][1]="Two"        
         myarray[0][2]="Three"        
         myarray[1][0]="Four"        
         myarray[1][1]="Five"        
         myarray[1][2]="Six"        
         myarray[2][0]="Seven"        
         myarray[2][1]="Eight"        
         myarray[2][2]="Nine"  
         document.write(myarray[2][0]);      
      </script>          
   </body>
</html>

Output

Seven

Updated on: 19-Jun-2020

512 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements