What Is [object Object] in JavaScript: Object.prototype.toString
A deeper explanation of [object Object]
Whilst working as a JavaScript developer, you might have used the toString method or heard of it at least.
And, if you are passionate to contribute your experiences to an open-source GitHub project, you would also have some opportunities to see how they check an argument’s type.
One day, I saw something that was quite weird — [object Object]. Um, I had no clue what it meant when I saw it for the first time.
I thought it was some sort of console mistake when printing data out, so I used to use console.dir, instead of console.log for objects.
Then, I saw this from MDN:
“Every object has a toString() method that is automatically called when the object is to be represented as a text value or when an object is referred to in a manner in which a string is expected.”
Prerequisites
To better understand what this post will talk about, you should know what prototype and prototype chain are in JavaScript.
TL;DR
Object.prototype.toString is a method that prints out the name of the class that you passed into toString.
There are several values you can get from it, based on ECMAScript’s specifications. This could be helpful when you check the type of a class of your object.
Types of JavaScript Objects
toString is a method for representing …what?
To understand what the heck this means, you should know what data types JavaScript has. Basically, JavaScript has seven primitive data types and other types, Object.
Some of them are instances of Object, and some aren’t. What toString prints out with each type is slightly different, but they all work under the same logic.
Let’s check out if there’s a way to see their type.
Except for Null and Undefined, every member of the list is an instance of the Object class.
If an object is an instance of Object, it means the object’s [[prototype]], which is __proto__ in most browsers, refers to Object.prototype as its final linked-place of the prototype chain.

Let’s look at this short example:


I created a random function class called worker whose method overrode Object.prototype.toString which prints something (we will deep-dive into it soon, don’t worry).
Then, I printed newWorker’s name by calling toString. So, why does this matter?
In JavaScript, any objects can possess their own methods, even though the name is redundant to q method from their super classes. Once you call toString in that example, the JavaScript engine starts to look for whether newWorker has toString in its scope.
If it does, the engine grabs it and invokes it. If it doesn’t, then the engine looks for [[prototype]](again, it’s __proto__ in most browsers) of newWorker and looks for toString in [[prototype]]’s scope.
If there is, then the engine grabs it and calls it and if there isn’t, it does the same thing when it looked for [[prototype]] of newWorker.
For the second instance, it’d be [[prototype]] of [[prototype]] of newWorker. The engine stops if [[prototype]] is null, when it reaches Object, or toString exists in a scope of this process.
This is called prototype-chaining.
[object Object] Is a Result of a Well-Trimmed Algorithm of Object.prototype.toString
Then what does Object.prototype.toString really do? It detects the class of an object and tells you what it is. Let’s look at some examples.













