<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abolfazl Soltani</title>
    <description>The latest articles on DEV Community by Abolfazl Soltani (@hookjs).</description>
    <link>https://dev.to/hookjs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1773834%2F46f34230-e6f3-4c7a-af6a-71a08631ff0a.JPEG</url>
      <title>DEV Community: Abolfazl Soltani</title>
      <link>https://dev.to/hookjs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hookjs"/>
    <language>en</language>
    <item>
      <title>[ object Object ]</title>
      <dc:creator>Abolfazl Soltani</dc:creator>
      <pubDate>Thu, 12 Jun 2025 00:50:57 +0000</pubDate>
      <link>https://dev.to/hookjs/-object-object--156f</link>
      <guid>https://dev.to/hookjs/-object-object--156f</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb281h1twxpgox8sm7o15.JPEG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb281h1twxpgox8sm7o15.JPEG" alt=" " width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We see two fields in this object that are part of the prototype of JavaScript objects.&lt;br&gt;
One of them is toString(), and the other is valueOf().&lt;/p&gt;

&lt;p&gt;If you’re a frontend developer, or have even written JavaScript once, you’ve probably seen [object Object] appear in your output or console logs.&lt;/p&gt;

&lt;p&gt;What is this mysterious string? Why do we see it instead of a useful string representation of our object? 🤔&lt;/p&gt;
&lt;h2&gt;
  
  
  What exactly is [object Object]?
&lt;/h2&gt;

&lt;p&gt;Despite appearances, [object Object] is not an array or some special string. It’s the default string representation of a plain JavaScript object.&lt;/p&gt;

&lt;p&gt;Here’s why:&lt;br&gt;
&lt;em&gt;JavaScript’s type system works on values, not variables. When you try to convert an object to a string or number (called type coercion), JavaScript internally uses the ToPrimitive algorithm to figure out how to do this.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;ToPrimitive tries to convert objects by calling two special methods on them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. toString()&lt;/li&gt;
&lt;li&gt;2. valueOf()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These methods help JavaScript decide how to represent the object as a primitive value (string or number).&lt;/p&gt;

&lt;p&gt;for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(obj &amp;lt; 50);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript assumes you want to convert obj to a number here.&lt;/p&gt;

&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(obj + '');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript assumes you want to convert obj to a string in this string concatenation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to learn more about ToPrimitive?
&lt;/h2&gt;

&lt;p&gt;You can read the official specification here:&lt;br&gt;
&lt;a href="https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toprimitive" rel="noopener noreferrer"&gt;ToPrimitive Algorithm — ECMAScript® 2024 Language Specification&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Why did I write this article?
&lt;/h2&gt;

&lt;p&gt;Because you can customize these conversion methods (&lt;strong&gt;toString()&lt;/strong&gt; and &lt;strong&gt;valueOf()&lt;/strong&gt;) yourself! 🤩&lt;/p&gt;

&lt;p&gt;Whether you use plain JavaScript or libraries, understanding and controlling this conversion is powerful. You can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Customize how your objects appear in logs or UI.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provide clearer debugging info — show exactly which object caused an error or was coerced implicitly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Manage unexpected JavaScript coercion behavior on your terms.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = {
  valueOf() { return 42; },
  toString() { return "hello"; }
};

console.log(obj + 1);  // Output: 43
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, because obj.valueOf() returns a primitive number, JavaScript uses that during addition.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Also, consider that if JavaScript calls either toString() or valueOf() during type conversion, but the returned value is still an object (not a primitive), then ToPrimitive will try the other method (valueOf() if it originally called toString(), or vice versa). If both methods return objects instead of primitives, JavaScript throws a TypeError.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;[object Object] is the default string from an object's toString() method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JavaScript uses toString() and valueOf() during type coercion with the ToPrimitive algorithm.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can override these methods for better control and debugging.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
