Home » Javascript

print_r in Javascript

8 January 2010 Share/Bookmark

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 as we have in PHP.

I googled the web and have found a very efficient print_r Javascript function. This has helped me a lot in my projects. 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://www.brandnewbox.co.uk/articles/details/a_print_r_equivalent_for_javascript/

From Mukesh Chapagain's Blog, post print_r in Javascript

email

php magento mukesh chapagain

Get New Post by Email
RSS Feed Subscribe RSS Feed
  • http://www.javascriptbank.com/javascript-countdown-timer.html Javascript Countdown Timer

    very cool & good code, thank you very much for sharing.

    Can you contribute this code on my JavaScript library?

    Awaiting your response. Thank

  • http://blog.chapagain.com.np Mukesh

    The source of this js code is from another site. I mentioned the link in the post. So, you can post to your js bank yourself.

  • Khalid Chauhdry

    Thanks Mukesh Chapagain it’s a good function

  • http://www.facebook.com/phat.reaction Andrew Killen

    Mukesh,

    I found when I setup your function if did not correctly place the UL elements around sub arrays in a multidimensional array.  

    So instead (used a bit of jquery to append write the output, but could just as easily be inner) I chose to add an extra function variable of return_value that will make the response be returned rather than echoed.  this way I was able to build the entire list before outputting…. 

    function print_r(theObj, return_value){
     var html = “”;
           if(theObj.constructor == Array || theObj.constructor == Object){
              for(var p in theObj){
                 if(theObj[p].constructor == Array || theObj[p].constructor == Object){
                 html += “["+p+"] => “+typeof(theObj);
                 html +=  print_r(theObj[p], true);
                 html += “”;                
                 } else {
                  html += “["+p+"] => “+theObj[p]+”";
                 }
              }
           }
           html += “”;
           if(return_value ==  undefined){
          $(‘#debug’).append(html);
          }
          else{
          return html;
          }
        }