JavaScript Print Object: console.log and JSON.stringify Examples

Learn how to print objects in JavaScript using console.log(), JSON.stringify(), console.table(), Object.keys(), Object.values(), and object entries.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

JavaScript Print Object: console.log and JSON.stringify Examples

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.

javascript
const user = { name: "Ana", role: "admin", active: true };

console.log("object-log:", user);
Output

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.

javascript
const user = { name: "Ana", role: "admin", active: true };

console.log("object-json:", JSON.stringify(user));
Output

You should see one line logging object-json: {"name":"Ana","role":"admin","active":true}.

For readable formatting, pass spacing as the third argument:

javascript
console.log(JSON.stringify(user, null, 2));
Output

Output:

json
{
  "name": "Ana",
  "role": "admin",
  "active": true
}

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.

javascript
const user = { name: "Ana", role: "admin", active: true };

console.log("object-values:", Object.values(user).join("|"));
Output

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.

javascript
const user = { name: "Ana", role: "admin", active: true };

console.log(Object.keys(user));
console.log(Object.entries(user));
Output

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 an 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.

javascript
const users = [
  { name: "Ana", role: "admin" },
  { name: "Ravi", role: "editor" },
];

console.table(users);
Output

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. 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.


Official Documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …