To create a multi line strings when printing to the JavaScript console, you need to add the new line character represented by the \n symbol. Alternatively, you can also add a new line break using Enter when you use the template literals syntax.
In JavaScript, the \n symbol stands for the newline character. If we need to print words in different lines on the console, we can use \n inside the string to introduce the line break.
If you want to do this to an object that has been already logged (one time thing), chrome console offers a good solution. Hover over the printed object in the console, right click, then click on "Store as Global Variable". Chrome will assign it to a temporary var name for you which you can use in the console.
stdout. write() method to print to console without trailing newline.
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);
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}
`);
Easy, \n
needs to be in the string.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With