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 :-)