This post will describe the [object, Object] in JavaScript.
What Does [object Object] Mean in JavaScript?
In JavaScript, “[object Object]” is the object’s default string representation. When you try to display an object in a string context, the browser will automatically call the toString() and display “[object Object]” instead of the object’s contents.
There are two scenarios in which such output is encountered:
Scenario 1: Display an Object Using “alert()” Method
If you try to print the object in an “alert()” method, it outputs “[object Object]” because the alert() method displays the string format.
Example
Create an object called “obj”:
name: 'Steven',
age: 18,
standard: 9
};
Call the “alert()” method to show the object’s properties:
The output displays “[object Object]” in an alert() method:
To fix this use the “JSON.stringify()” method that change the object into the string that will display in the alert() method:
Scenario 2: Call the “toString()” Method on an Object
If you can try to print the object by converting it to the string using the “toString()” method, it prints the “[object Object]”:
Output
To fix this, just call the object into the log() method:
It will print the whole object on the console:
That’s all about the [object Object] in JavaScript.
Conclusion
The “[object Object]” is the object’s default string representation. To print the content of an object, use the “JSON.stringify()” method instead of the “alert()” method. It first converts the object into a string and then displays it on the alert() message. If you want to display the object’s content on the console, use the “console.log(obj)” instead of the “console.log(obj.toString())” method. This post described the meaning of [object, Object] in JavaScript.