Print content of JavaScript object? [duplicate]

Print content of JavaScript object? [duplicate]

This question already has an answer here:

How can I display a JavaScript object?

36 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?


Solution 1:

If you are using Firefox, alert(object.toSource()) should suffice for simple debugging purposes.

Solution 2:

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.

Solution 3:

Aside from using a debugger, you can also access all elements of an object using a foreach loop. The following printObject function should alert() your object showing all properties and respective values.

function printObject(o) {
  var out = '';
  for (var p in o) {
    out += p + ': ' + o[p] + '\n';

// now test it:
var myObject = {'something': 1, 'other thing': 2};

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.

Solution 4:

If you just want to have a string representation of an object, you could use the JSON.stringify function, using a JSON library.

Solution 5:

Print content of object you can use


you can see the result in console like below.

Object {description: "test"} 

For open console press F12 in chrome browser, you will found console tab in debug mode.

Solution 6:

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.

$ node

var obj = {
   "name" : "John",
   "surname" : "Doe"
obj.self_ref = obj;

util = require("util");

var obj_str = util.inspect(obj);
// prints { name: 'John', surname: 'Doe', self_ref: [Circular] }

It that case JSON.stringify throws exception: TypeError: Converting circular structure to JSON