print_r in Javascript

It’s a real headache when you have to work on objects and arrays in Javascript. It would be lot easier to detect elements of the Javascript objects/arrays if we have print_r function in Javascript like we have in PHP.

I googled the web and have found a very efficient print_r Javascript function. Here is the code:-


<script type="text/javascript">
	function print_r(theObj){ 
	   if(theObj.constructor == Array || theObj.constructor == Object){ 
		  document.write("<ul>") 
		  for(var p in theObj){ 
			 if(theObj[p].constructor == Array || theObj[p].constructor == Object){ 
				document.write("<li>["+p+"] => "+typeof(theObj)+"</li>"); 
				document.write("<ul>") 
				print_r(theObj[p]); 
				document.write("</ul>") 
			 } else { 
				document.write("<li>["+p+"] => "+theObj[p]+"</li>"); 
			 } 
		  } 
		  document.write("</ul>") 
	   } 
	} 
</script> 

USING PRINT_R


<script type="text/javascript">
print_r(JAVACRIPT_ARRAY_OR_OBJECT);
</script> 

Source: http://brandnewbox.co.uk/articles/details/a_print_r_equivalent_for_javascript