In Node.js is there any way to print the \n \r on console?

Amol M Kulkarni picture Amol M Kulkarni · Jun 27, 2013 · Viewed 11k times · Source

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?

Answer

deoxxa picture deoxxa · Jun 27, 2013

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!"