[object Object] is a string representation of an object. You may see this text if you use alert() to print an object to the screen, for instance. You can view the contents of an object using console.log(), JSON.stringify(), or a for…in loop.
When developing using JavaScript, many of us have encountered the output: [object Object]. When I saw this the first time, I went to my mentor at the time and asked: “What does this even mean?”. I was confused.
This article aims to tell you about this output and what it means. We’ll talk about how you can translate [object Object] into readable content that you can work with.
What is JavaScript [object Object]?
[object Object] is a string version of an object instance. This value is returned by a JavaScript program if you try to print out an object without first formatting the object as a string.
This is the syntax for the [object Object] object:
[object Object]
It’s no wonder developers get confused about this object: there are no error messages or warnings that tell us what is going on. Let’s take a look at an example of this object in action.
[object Object] JavaScript Example
Take this example:
let objA = { name: "christina", degree: "music", instrument: "flute" } alert(objA);
When the alert() statement runs, our code returns [object Object]. Our program tries to return a string representation of what was passed into the alert() method. But, because our code sees this as an object, it tells us that it’s an instance of an Object instead.
The [object Object] message is not very descriptive. But, this does not mean that we cannot see values in our object. Let’s talk about the ways in which we can read the values in an object.
What’s Inside the Object?
Knowing that [object Object] is an instance of an Object is great, but we want to know is inside the object. There are three ways to do that:
- Log to console with console.log(<your-object-name>)
- Stringify it with JSON.stringify(<your-object-name>)
- Use for…in loop and look at each individual property
Log to the Console
Arguably the easiest way to see what is inside an object is to log the object to the console. The console.log() statement lets you view all of the values in a JavaScript object.
Consider the following code:
let objA = { name: "christina", degree: "music", instrument: "flute" } console.log(objA);
Our code declares an object called objA. Then, we print out the value of the object to the console. Our code returns:
{ degree: "music", instrument: "flute", name: "christina" }
We can see the values in our object.
Use JSON.stringify()
The JSON.stringify() method converts a JavaScript object to a string. We can then manipulate this string.
So, we can use JSON.stringify() to convert an object to a string. Then, we could use alert() to display the value of the string to the user:
let objA = { name: "christina", degree: "music", instrument: "flute" } alert(JSON.stringify(objA));
Like in our last example, we have defined an object called objA. Then, we use the JSON.stringify() method to convert the object to a string. We then use alert to display the value of the string to the console.
Our code opens up a prompt box with the following contents:
{"name":"christina","degree":"music","instrument":"flute"}
Use a for…in Loop
The JavaScript for…in loop lets us iterate over the contents of an object. We can use this loop to print out each individual key-value pair.
Consider the following code:
let objA = { name: "christina", degree: "music", instrument: "flute" } for(let key in objA) { console.log(key + ":", objA[key]); }
We have declared a JSON object called objA like we did in the last two examples. Then, we use a for…in loop to iterate over the contents of this object. The “key” value represents each key.
We use the “key” value to access the key and objA[key] to access the value associated with that key. Our code returns:
"name:christina" "degree:music" "instrument:flute"
We use string concatenation to add a colon (:) between each key and value. This lets us separate the keys and values so they are more readable in the output of our code.
Conclusion
The JavaScript [object Object] is a string representation of an object. To see the contents of an object, you should print the object to the console using console.log() or convert the object to a string. Or, you can use a for…in loop to iterate over the object and see its contents.
Are you interested in learning more about JavaScript? Check out our complete How to Learn JavaScript guide for advice on top learning resources and online courses.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.