Converting an object to a string

后端 未结 30 1890
北荒
北荒 2020-11-22 03:29

How can I convert a JavaScript object into a string?

Example:

var o = {a:1, b:2}
console.log(o)
console.log(\'Item: \' + o)

Output:

30条回答
  •  死守一世寂寞
    2020-11-22 04:22

    One option:

    console.log('Item: ' + JSON.stringify(o));

    o is printed as a string

    Another option (as soktinpk pointed out in the comments), and better for console debugging IMO:

    console.log('Item: ', o);

    o is printed as an object, which you could drill down if you had more fields

提交回复
热议问题