When tracing out variables in the console, How to create a new line?

后端 未结 7 1280
孤街浪徒
孤街浪徒 2021-01-30 03:39

So I\'m trying to do something simple, I want to break up my traces in the console into several lines, using 1 console.log statement:

console.log(\'roleName = \'         


        
相关标签:
7条回答
  • 2021-01-30 04:04

    The worst thing of using just

    console.log({'some stuff': 2} + '\n' + 'something')
    

    is that all stuff are converted to the string and if you need object to show you may see next:

    [object Object]
    

    Thus my variant is the next code:

    console.log({'some stuff': 2},'\n' + 'something');
    
    0 讨论(0)
  • 2021-01-30 04:12

    In ES6/ES2015 you can use string literal syntax called template literals. Template strings use backtick character instead of single quote ' or double quote marks ". They also preserve new line and tab

    const roleName = 'test1';
    const role_ID = 'test2';
    const modal_ID = 'test3';
    const related = 'test4';
            
    console.log(`
      roleName = ${roleName}
      role_ID = ${role_ID}
      modal_ID = ${modal_ID}
      related = ${related}
    `);

    0 讨论(0)
  • 2021-01-30 04:14

    You should include it inside quotes '\n', See below,

    console.log('roleName = '+roleName+ '\n' + 
                 'role_ID = '+role_ID+  '\n' + 
                 'modal_ID = '+modal_ID+ '\n' +  
                 'related = '+related);
    
    0 讨论(0)
  • 2021-01-30 04:15

    Why not just use separate console.log() for each var, and separate with a comma rather than converting them all to strings? That would give you separate lines, AND give you the true value of each variable rather than the string representation of each (assuming they may not all be strings).

    console.log('roleName',roleName);
    console.log('role_ID',role_ID);
    console.log('modal_ID',modal_ID);
    console.log('related',related);
    

    And I think it would be easier to read/maintain.

    0 讨论(0)
  • 2021-01-30 04:16

    Easy, \n needs to be in the string.

    0 讨论(0)
  • 2021-01-30 04:24
    console.log('Hello, \n' + 
                'Text under your Header\n' + 
                '-------------------------\n' + 
                'More Text\n' +
                'Moree Text\n' +
                'Moooooer Text\n' );
    

    This works great for me for text only, and easy on the eye.

    0 讨论(0)
提交回复
热议问题