Minor note: I'm very familiar with console.log()
, JSON.stringify()
, Object.prototype.toString()
, util.inspect()
etc - this question isn't asking how to show the contents of objects, but rather why node's behavior changes in different circumstances.
I know I can console.log(someObject)
and node will print:
[object Object]
node js function return [object Object] instead of a string value has some good information on this.
I know that [object Object]
is from Object.prototype.toString()
I know I can console.log(JSON.stringify(someObject, null, 2)
and node will print:
{
foo: 'bar'
}
Or use util.inspect()
etc. See this answer
However it seems that sometimes node will actually print the objects contents
If I make a new file called runme.js
with the contents:
console.log({foo: 'bar'})
And run node runme.js
node will print
{ foo: 'bar' }
Not [object Object]
Why is node not printing [object Object]
?
Edit: per Keith's question, [object Object]
will appear when I run:
console.log(`Check me out ${{foo: 'bar'}}`)
Logs [object Object]
What determines whether node uses Object.prototype.toString()
(aka [object Object]
) vs printing the contents of the object?