This question already has an answer here:
- How can I display a JavaScript object? 35 answers
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?
This question already has an answer here:
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?
Print content of object you can use
you can see the result in console like below.
For open console press F12 in chrome browser, you will found console tab in debug mode.
This will give you very nice output with indented JSON object:
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.
You should consider using FireBug for JavaScript debugging. It will let you interactively inspect all of your variables, and even step through functions.
Aside from using a debugger, you can also access all elements of an object using a
foreach
loop. The followingprintObject
function shouldalert()
your object showing all properties and respective values.Using a DOM inspection tool is preferable because it allows you to dig under the properties that are objects themselves. Firefox has FireBug but all other major browsers (IE, Chrome, Safari) also have debugging tools built-in that you should check.
You could Node's util.inspect(object) to print out object's structure.
It is especially helpful when your object has circular dependencies e.g.
It that case JSON.stringify throws exception:
TypeError: Converting circular structure to JSON
Use dir(object). Or you can always download Firebug for Firefox (really helpful).