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