How do i pretty print javascript objects/variables?

This question already has an answer here:

  • How can I pretty-print JSON using JavaScript? 20 answers

  • Change the above example to:

    var user = {
      first: 'Jack',
      last: 'Green',
      age: 54
    };
    
    // plain console log
    console.log(JSON.stringify(user, undefined, 2));
    
    // or with interpolation:
    console.log(`User: ${JSON.stringify(user, undefined, 2)}`);
    

    and now we get the nice looking output:

    {
      "first": "Jack",
      "last": "Green",
      "age": 54
    }
    
    User: {
      "first": "Jack",
      "last": "Green",
      "age": 54
    }
    
    链接地址: http://www.djcxy.com/p/8452.html

    上一篇: 重复字符串

    下一篇: 我如何打印JavaScript对象/变量?