Skip to content

Console log shows object object | JSON stringify

  • by

Use console.log(JSON.stringify(result)) to get the JSON in a string format. Or to avoid the [object Object] error use the if statement to check whether the property exists or not.

Mostly Console log doesn’t show object object, if you are using the alert box then it could possible.

The example console log shows object object

Simple example with error undefined.

<script>
   var obj = {id : "007", name : "James Bond"};
   console.log(obj);                    
     
   console.log(obj.movie);             
 </script>

Solution

<!DOCTYPE html>
<html>
<body>

<script>
   var obj = {id : "007", name : "James Bond"};
   console.log(obj);                    
   console.log(JSON.stringify(obj));  
    
   if (obj.hasOwnProperty("id")){
    console.log(obj.id);      
  }       
</script>

</body>
</html>

Output:

Console log shows object object

[object Object]: What does this mean?

Answer: [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.

<script>
   let objA = {
     name: "christina",
     degree: "music",
     instrument: "flute"
   }
   
   alert(objA);     
 </script>

Do comment if you have any doubts or suggestions on this JS console log topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading