Question

Consider, the following code:

var myVar = 'This is my text.\nAnd other information.\rHow to console.log\n\r?';
console.log(myVar);

Output:

This is my text.
And other information.
How to console.log
?

How can I console with the \n, \r & \n\r?

Was it helpful?

Solution

I find myself using JSON.stringify to print things like this.

Code:

console.log(JSON.stringify("i am a string!\nwith some newlines\r\n!"));

Output:

"i am a string!\nwith some newlines\r\n!"

OTHER TIPS

Got some solution:

Code:

var myVar = 'This is my text.\nAnd other information.\rHow to console.log\n\r?';
console.log(require('util').inspect(myVar, { showHidden: true, depth: null }));
console.log('----------------');
console.log('Just console.log');
console.log(myVar);

Output:

This is my text.\nAnd other information.\rHow to console.log\n\r?
----------------
Just console.log
This is my text.
And other information.
How to console.log
?

Other ways are also welcomed.

This is an old question, but for those who land on this page like I did, a simpler solution is just to escape the delimeter with a preceding "\".

console.log("I want to print \\n instead of it making a new line.")

It's possible. juste use

process.stdout.write('This is my text.\nAnd other information.\rHow to console.log\n\r?')

instead of

console.log('This is my text.\nAnd other information.\rHow to console.log\n\r?')

as far as I know, if you output some special chars in your console.log

example : % is in fact a control caracter for console log you have to urlencode it to : %25

because %c does the css formating of your output, as an example

the line feeds will not be displayed as so

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top