This question already has an answer here:
I would like to see the structure of object in JavaScript (for debugging). Is there anything similar to var_dump in PHP?
This question already has an answer here:
I would like to see the structure of object in JavaScript (for debugging). Is there anything similar to var_dump in PHP?
Most common way:
However I must mention
JSON.stringify
which is useful to dump variables in non-browser scripts:The
JSON.stringify
function also supports built-in prettification as pointed out by Simon Zyx.Example:
The above snippet will print:
On caniuse.com you can view the browsers that support natively the
JSON.stringify
function: http://caniuse.com/jsonYou can also use the Douglas Crockford library to add
JSON.stringify
support on old browsers: https://github.com/douglascrockford/JSON-jsDocs for
JSON.stringify
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringifyI hope this helps :-)
A nice simple solution for parsing a JSON Response to HTML.
I wrote this JS function
dump()
to work like PHP'svar_dump()
. To show the contents of the variable in an alert window:dump(variable)
To show the contents of the variable in the web page:dump(variable, 'body')
To just get a string of the variable:dump(variable, 'none')
I put this forward to help anyone needing something readily practical for giving you a nice, prettified (indented) picture of a JS
Node
. None of the other solutions worked for me for aNode
("cyclical error" or whatever...). This walks you through the tree under the DOMNode
(without using recursion) and gives you the depth,tagName
(if applicable) andtextContent
(if applicable).Any other details from the nodes you encounter as you walk the tree under the head node can be added as per your interest...
You could also try this function. Can't remember the original author, but all credits goes to him/her.
Works like a charm - 100% the same as var_dump in PHP.
Check it out.
Firebug.
Then, in your javascript:
Now you can look at the console, click on the statement and see what is inside
blah