javascript how to detect browser type | pontikis | php datetime now | autocomplete ajax


What is the JavaScript Equivalent of print_r in PHP?

What is the JavaScript Equivalent of PHP print_r() function? In other words, how you can "print" a javascript object in a way that's readable by humans?


You could use JSON.stringify, as following:

The HTML part

Let's create two links for demo:

<p><a id="print_demo" href="javascript:void(0);">Print object</a></p>

<p><a id="pretty_print_demo" href="javascript:void(0);">Pretty Print object</a>	


Remark: I use jQuery to handle events, but you can use plain javascript (if you prefer).

<script type="text/javascript">

	$(function() {

		// create an object
		var person = new Object();
		person.firstname = "John";
		person.lastname = "Doe";
		person.age = "35";

		// plain print
		$("#print_demo").click(function() {

		// pretty print
		$("#pretty_print_demo").click(function() {
			alert(JSON.stringify(person, null, '    '));




Using the above code:

Click to Print object

Click to Pretty Print object

About Internet Explorer

JSON will not work (by default) on Microsoft Internet Explorer ≤ 9. For a cross browser implementation, get json2.js from and use the following code:

<!--[if lt IE 10]>
<script type="text/javascript" src="json2.js"></script>

Another alternative is json3 from


From JSON docs

From Mozilla docs

Sign-up for our free email newsletter. Get updates when new tutorials and tips are published. You can unsubscribe anytime with a click.

Your comments are welcomed!

This site actively encourages commenting on any post. Comments are not pre-moderated, but this community does not tolerate direct or indirect attacks, name-calling or insults. Please, read terms of use and Comment Policy at privacy policy.

More tips