Print content of JavaScript object?

cometta picture cometta · Oct 26, 2009 · Viewed 629.2k times · Source

Typically if we just use alert(object); it will show as [object Object]. How to print all the content parameters of an object in JavaScript?

Answer

Igor Jerosimić picture Igor Jerosimić · Dec 23, 2011

This will give you very nice output with indented JSON object:

alert(JSON.stringify(YOUR_OBJECT_HERE, null, 4));

The second argument alters the contents of the string before returning it. The third argument specifies how many spaces to use as white space for readability.