I’m running this in node.js:
> x = { 'foo' : 'bar' }
{ foo: 'bar' }
> console.log(x)
{ foo: 'bar' }
undefined
> console.log("hmm: " + x)
hmm: [object Object]
undefined
What I don’t understand is why console.log(x) “pretty-prints” the object, whereas string concatenation “ugly-prints” it. And more importantly, what’s the best way to make it print hmm: { foo: 'bar' }?
The
+ xcoerces the objectxinto a string, which is just[object Object]:http://jsfiddle.net/Ze32g/
The pretty printing is a very nice and probably very complex underlying code that someone implemented as part of the
consoleobject and thelogmethod.Try this: