JavaScript

What Does [object Object] Mean in JavaScript

Developers may have encountered the “[object, Object]” result while working with objects in JavaScript. It is the object’s default string representation in JavaScript. It is typically utilized to indicate that an object is being used in a context where a string is expected, such as an alert() method. This can appear irrelevant, but this is not necessarily an error.

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”:

const obj = {

name: 'Steven',

age: 18,

standard: 9

};

Call the “alert()” method to show the object’s properties:

alert(obj);

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:

alert(JSON.stringify(obj));

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]”:

console.log(obj.toString());

Output

To fix this, just call the object into the log() method:

console.log(obj);

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.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.