How to print object array in JavaScript

No comments
Whether you are working first time on JavaScript technology or you are a full time JavaScript developer, in your day to day development sometimes, you need to print object array to see the array values and to take your next steps.

In this tutorial, i am going to show you multiple methods of printing object array:

How to print array in HTML tag in JavaScript?

In below example, I have used JavaScript JSON.stringify() function that is used to convert object into a string.
 <div id="abc"></div>  
 <script type="text/javascript">  
 var mycity=new Array();   
  mycity[0]="Delhi";      
  mycity[1]="Banglore";  
  mycity[2]="Mombai";  
  document.getElementById('abc').innerHTML = JSON.stringify(mycity);  
 </script>  

Output:
 ["Delhi","Banglore","Mombai"]  

How to print JavaScript array using Console.log?

 If you are debugging your code and want to print array using console.log() function. See the code below:
 console.log(mycity);  


No comments :

Post a Comment