Back to KB
Difficulty
Intermediate
Read Time
8 min

How can I correctly print object information in logs when console.log shows only [object Object]?

By Codcompass TeamΒ·Β·8 min read

Mastering Object Serialization in JavaScript Logging: Beyond the [object Object] Trap

Current Situation Analysis

Modern JavaScript and TypeScript applications routinely handle complex nested payloads: API responses, state trees, telemetry events, and form data. When developers attempt to inspect these structures using standard console output, they frequently encounter a deceptively simple problem: the log displays [object Object] instead of the actual data. This issue is not a bug in the runtime; it is a direct consequence of how template literals and string interpolation interact with JavaScript's type coercion rules.

The root cause lies in Object.prototype.toString(). When an object is placed inside a template literal (e.g., `Payload: ${data}`), the JavaScript engine implicitly invokes the object's toString() method to satisfy the string context. For plain objects, arrays, and most custom classes, this method returns the literal string [object Object]. While this behavior is consistent with ECMAScript specifications, it clashes with modern debugging expectations where developers assume the console will automatically pretty-print or inspect complex types.

This problem is frequently overlooked because:

  1. Convenience over correctness: Template literals are the default string formatting mechanism in modern codebases. Developers rarely inspect the underlying coercion mechanics.
  2. Console API ambiguity: console.log(obj) actually prints an interactive object reference in most devtools, but console.log(`${obj}`) forces string conversion. The subtle difference leads to inconsistent logging patterns across teams.
  3. Production blind spots: In staging or production environments, console output is often piped to log aggregators (Datadog, Splunk, CloudWatch). These systems receive raw strings, meaning [object Object] becomes permanent, unsearchable noise in telemetry pipelines.

Engineering teams consistently report that deciphering malformed logs consumes 15–20% of debugging time. When payloads contain nested structures, missing fields, or circular references, the lack of proper serialization strategy directly impacts incident response velocity and system observability.

WOW Moment: Key Findings

The choice of logging strategy fundamentally alters debugging efficiency, runtime performance, and data safety. Below is a comparative analysis of the four primary approaches used in production TypeScript environments.

ApproachReadabilityCircular Ref SafetyPerformance Overhead
Implicit String CoercionPoor ([object Object])N/A (fails silently)Negligible
Direct JSON.stringify()HighFails (throws TypeError)Moderate
Safe Serialization (WeakSet Replacer)HighHandled gracefullyLow-Moderate
Targeted Field ExtractionMedium-HighN/A (bypasses serialization)Minimal

Why this matters: Implicit coercion provides zero visibility. Direct serialization breaks on circular references, which are common in DOM trees, framework state, or bidirectional API responses. Safe serialization preserves structure while preventing runtime crashes, but introduces measurable CPU overhead in hot paths. Targeted field extraction sacrifices full payload visibility for deterministic performance and security compliance. Understanding these trade-offs allows engineering teams to align logging strategies with environment constraints (dev vs. prod), performance budgets, and data governance policies.

Core Solution

Resolving the [object Object] issue requires shifting from a

πŸŽ‰ Mid-Year Sale β€” Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register β€” Start Free Trial

7-day free trial Β· Cancel anytime Β· 30-day money-back