Javascript console.log(object) vs. concatenating string

后端 未结 5 651
孤城傲影
孤城傲影 2020-12-03 02:26

I\'m running this in node.js:

> x = { \'foo\' : \'bar\' }
{ foo: \'bar\' }
> console.log(x)
{ foo: \'bar\' }
undefined
> console.log(\"hmm: \" + x)
         


        
5条回答
  •  天涯浪人
    2020-12-03 02:58

    The + x coerces the object x into 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 console object and the log method.

    Try this:

    console.log("hmm: ", x);
    

提交回复
热议问题