Print an Object in JavaScript: A Practical Guide
Learn how to print an object in JavaScript with clear, readable outputs. Explore console.log, JSON.stringify, console.dir, console.table, and custom pretty-printers for browser and Node environments.

In this how-to, you will learn to print an object in JavaScript using multiple approaches (console.log, console.dir, JSON.stringify, and console.table) for readable debugging across browser and Node.js. You’ll compare their formatting, limitations, and best use cases.
Why print an object javascript matters
Printing objects is essential for debugging, data inspection, and communicating program state during development. In practice, developers rely on console outputs to understand complex shapes, nested properties, and runtime values. According to JavaScripting, choosing the right printing approach reduces debugging time and clarifies code intent. The JavaScripting team found that beginners often start with a simple console.log and quickly outgrow it when objects become deeply nested or circular. This guide helps you pick the right tool for browser or Node.js environments and explains how to tailor outputs for readability.
Printing techniques: toString, JSON.stringify, and console APIs
JavaScript offers several ways to serialize and present objects. The default toString method on Object.prototype is often not helpful, returning a generic [object Object] string unless overridden. JSON.stringify converts an object to a JSON text, which is great for data transmission but omits functions and may fail on circular references. Console APIs like console.log, console.dir, and console.table provide rich, interactive views in browsers and Node.js. Use JSON.stringify with a replacer or spaces parameter to control formatting and filter properties when needed. As you print, remember the keyword print an object javascript and how different methods reveal different facets of the data.
Handling deep or circular structures safely
Deeply nested objects can be hard to read, and circular references crash naive JSON.stringify calls. Use a replacer function or a custom serializer to limit depth, or adopt libraries that detect cycles. In Node.js, util.inspect offers options to control depth, colors, and compactness for easier reading. In browsers, console.dir often presents expandable trees that let you explore properties on demand. When dealing with user data or sensitive fields, avoid printing everything; selectively log only what’s necessary to diagnose issues.
Formatting outputs for debugging: pretty printing and custom formatters
For readable prints, pretty-print with JSON.stringify(obj, null, 2) to indent with two spaces. In Node.js, util.inspect(obj, { depth: null, colors: true }) yields colored, structured output. Browser consoles can render objects interactively, but you may still want string representations for logs or files. When printing arrays or lists of objects, console.table provides a compact tabular view that can improve scanning of properties across many items. This section shows how to combine these techniques for consistent, maintainable debug outputs.
Practical examples: code snippets for common objects
const user = { id: 1, name: 'Alex', role: 'dev', prefs: { theme: 'dark', itemsPerPage: 10 } };
console.log('user object:', user); // raw print
console.dir(user, { depth: 2, colors: true }); // interactive in browser/Node
console.table([user]); // tabular view when printing arrays
console.log('stringified with indentation:', JSON.stringify(user, null, 2));The examples show how different methods present the same data. For nested structures, prefer JSON.stringify with a depth limiter or a custom replacer to avoid flooding logs with irrelevant details. Remember print an object javascript in your notes as you test.
Performance and readability trade-offs
Printing objects frequently can slow down dashboards or logs if the data is large. Prefer targeted prints that surface only the properties you need for debugging. When shipping code to production, disable verbose prints or guard them behind a debug flag. Always balance readability with performance; a concise, well-formatted print is usually better than a verbose dump that obscures the root cause.
Debugging workflows: recommended approach
- Identify the object you need to inspect. 2) Choose a printing method based on environment (console.table for arrays, JSON.stringify for structured data, util.inspect for Node). 3) Use a replacer to omit or transform sensitive fields. 4) Validate the output against expectations, and remove or gate prints before release. This workflow helps maintain clarity and avoids noisy logs.
Best practices and gotchas
- Avoid printing large objects in production logs; mask or omit sensitive data.
- Beware circular references; JSON.stringify will fail unless you handle cycles.
- Prefer environment-aware printing so browser-specific APIs don’t break in Node.
- Document your chosen approach so teammates reuse consistent patterns.
- The JavaScripting team recommends adopting a small utility to standardize object printing across projects.
Tools & Materials
- Code editor (e.g., VS Code)(Open a new .js file for testing printing functions)
- Node.js runtime(Run tests with node test.js and inspect console output)
- Browser developer tools(Use Console, Elements, and Network panels)
- Optional: JSON viewer extension(Helps inspect JSON output visually)
- Sample data objects(Create representative objects to test printing)
Steps
Estimated time: 60-90 minutes
- 1
Install a runtime
Install Node.js from the official site and verify the installation with node -v. This ensures you can run and test printing outside the browser.
Tip: Restart your terminal after installation. - 2
Create a test file
Create a new JavaScript file (test.js) to house your printing examples and sample objects.
Tip: Keep the file in a dedicated tests/ folder. - 3
Print with console.log
Add a simple console.log(obj) to see the default object representation in your environment.
Tip: Use console.log with a label to avoid ambiguity. - 4
Use console.dir for depth
Call console.dir(obj, { depth: 2, colors: true }) to inspect nested properties clearly.
Tip: Increase depth only as needed to avoid clutter. - 5
Pretty-print with JSON.stringify
Print JSON.stringify(obj, null, 2) to format output with indentation.
Tip: Be mindful that functions are omitted by JSON.stringify. - 6
Handle circular references
If obj contains cycles, JSON.stringify will fail—use a replacer or a safe serializer.
Tip: Consider a small cycles-safe utility for production code. - 7
Print tabular data
For arrays of objects, use console.table(data) to present a table-like view.
Tip: Pass an array of objects with uniform keys. - 8
Cross-environment checks
Detect environment (browser vs Node) and choose the best printing method accordingly.
Tip: Use typeof window !== 'undefined' to hint at browser. - 9
Consolidate into a utility
Wrap printing logic in a tiny utility function to reuse across projects.
Tip: Document the API of your utility for team usage.
Questions & Answers
What is the difference between console.log and JSON.stringify when printing objects?
console.log prints the object in the console in a live, expandable form, while JSON.stringify converts it into a text JSON representation. The latter is portable but omits functions and may fail on circular references.
console.log shows a live object; JSON.stringify gives a JSON string, which is portable but excludes functions and can fail on cycles.
How can I print objects with circular references safely?
JSON.stringify will throw on circular references. Use a replacer, a safe serializer, or a library that detects cycles. In Node.js, you can also use util.inspect with a depth limit.
Circular references break JSON.stringify; use a safe serializer or library to print safely.
When should I avoid logging sensitive data?
Never print sensitive data in production logs. Use redaction or selective logging to protect user information and credentials.
Be careful not to leak sensitive data in logs; redact as needed.
Is console.table useful for objects?
Yes. console.table is especially helpful for arrays of objects, letting you compare fields across many items at a glance.
console.table is great for comparing object lists in a table.
Can I pretty-print objects in the browser console?
Yes. Browser consoles support expandable trees via console.dir and colorized output for readability when supported.
You can pretty-print in the browser console using console.dir.
What is a recommended workflow for printing during debugging?
Identify the data, choose an appropriate print method, log only what’s needed, and remove prints before shipping code.
Use a consistent workflow: choose a method, log what you need, remove prints later.
Watch Video
What to Remember
- Use JSON.stringify for stable object dumps
- Choose the printing method by environment
- Avoid exposing sensitive fields in logs
- Create a small utility to standardize prints
