Printing an object in JavaScript usually means sending it to the console for debugging or converting it to a readable string for logs, HTML, files, or API output. The best method depends on whether you want an interactive console object, formatted JSON, only values, or a table view.
This guide covers the common ways to print a JavaScript object with console.log(), JSON.stringify(), console.table(), Object.values(), and Object.entries().
Environment: Node.js v20.18.2. After each runnable snippet, the following paragraph states the expected console output (order and values).
Method 1: Print an Object with console.log()
Use console.log() when debugging in Node.js or browser developer tools.
const user = { name: "Ana", role: "admin", active: true };
console.log("object-log:", user);You should see one line logging object-log: { name: 'Ana', role: 'admin', active: true }.
In browser developer tools, logged objects may be interactive and expandable. In Node.js, the object is printed as text using Node's console formatting.
Method 2: Print an Object as JSON with JSON.stringify()
Use JSON.stringify() when you need a string version of the object.
const user = { name: "Ana", role: "admin", active: true };
console.log("object-json:", JSON.stringify(user));You should see one line logging object-json: {"name":"Ana","role":"admin","active":true}.
For readable formatting, pass spacing as the third argument:
console.log(JSON.stringify(user, null, 2));Output:
{
"name": "Ana",
"role": "admin",
"active": true
}For indentation strings and replacers that select which fields appear,
see how to pretty-print with JSON.stringify().
JSON.stringify() is ideal for logs and storage, but it skips functions
and converts unsupported values such as undefined differently
depending on where they appear.
Method 3: Print Object Values
Use Object.values() when you only need the values from an object.
const user = { name: "Ana", role: "admin", active: true };
console.log("object-values:", Object.values(user).join("|"));You should see one line logging object-values: Ana|admin|true.
This is useful when converting object data into a row, CSV-like output, or a simple display string.
Method 4: Print Object Keys and Entries
Use Object.keys() for property names and Object.entries() for key-value pairs.
const user = { name: "Ana", role: "admin", active: true };
console.log(Object.keys(user));
console.log(Object.entries(user));You should see 2 lines, in order: [ 'name', 'role', 'active' ], [ [ 'name', 'Ana' ], [ 'role', 'admin' ], [ 'active', true ] ].
Object.entries() is often the cleanest option when you want to
loop through a JavaScript object and
print each key with its value.
Method 5: Print Objects in a Table
Use console.table() for arrays of objects or objects that represent rows.
const users = [
{ name: "Ana", role: "admin" },
{ name: "Ravi", role: "editor" },
];
console.table(users);This is especially helpful in browser developer tools and Node.js terminals when comparing multiple objects with the same properties.
Common Questions About Printing Objects in JavaScript
Why does JavaScript print [object Object]?
[object Object] appears when an object is converted to a string
implicitly, such as with string concatenation. This comes from the
default JavaScript toString() behavior. Use
console.log(object) for debugging or JSON.stringify(object) when you
need a readable string.
What is the best way to print a nested object?
Use console.log() for an expandable debug view or JSON.stringify(obj, null, 2) for formatted JSON. For very deep Node.js objects, console.dir(obj, { depth: null }) can also help.
Can JSON.stringify print every JavaScript object?
No. JSON.stringify() cannot serialize circular references and does not preserve functions, symbols, or undefined object properties. It is best for JSON-compatible data.
Summary
Use console.log() to print objects while debugging, JSON.stringify() to produce JSON text, Object.values() or Object.entries() to print selected object data, and console.table() to compare arrays of objects. For production logs, prefer stable serialized output; for debugging, keep the object form so developer tools can inspect it interactively.
