I would like to see the structure of object in JavaScript (for debugging). Is there anything similar to var_dump in PHP?
The var_dump equivalent in JavaScript? Simply, there isn't one. Prints an interactive listing of all properties of the object. This looks identical to the view that you would see in the DOM tab.
The function var_dump() displays structured information (type and value) about one or more expressions/variables. Arrays and objects are explored recursively with values indented to show structure. All public, private and protected properties of objects will be returned in the output.
var_dump() displays values along with data types as output. print_r() displays only value as output. It does not have any return type. It will return a value that is in string format.
The var_dump() function dumps information about one or more variables. The information holds type and value of the variable(s).
Most modern browsers have a console in their developer tools, useful for this sort of debugging.
console.log(myvar);
Then you will get a nicely mapped out interface of the object/whatever in the console.
Check out the console
documentation for more details.
Most common way:
console.log(object);
However I must mention JSON.stringify
which is useful to dump variables in non-browser scripts:
console.log( JSON.stringify(object) );
The JSON.stringify
function also supports built-in prettification as pointed out by Simon Zyx.
Example:
var obj = {x: 1, y: 2, z: 3}; console.log( JSON.stringify(obj, null, 2) ); // spacing level = 2
The above snippet will print:
{ "x": 1, "y": 2, "z": 3 }
On caniuse.com you can view the browsers that support natively the JSON.stringify
function: http://caniuse.com/json
You can also use the Douglas Crockford library to add JSON.stringify
support on old browsers: https://github.com/douglascrockford/JSON-js
Docs for JSON.stringify
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
I hope this helps :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With