Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 821
Amol M Kulkarni Avatar asked Jun 27 '13 07:06

Amol M Kulkarni


People also ask

How do I print a new line in console log?

If you wish to add a new line to the JavaScript new line console while printing a few words, the \n sign for the new line can be used.

How do I print a node JS console?

log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.

How do I get rid of the next line in console log?

But sometimes we may need to print the console without trailing newline. In that case, we can use process. stdout. write() method to print to console without trailing newline.


2 Answers

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!"
like image 152
deoxxa Avatar answered Sep 17 '22 11:09

deoxxa


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.

like image 32
Amol M Kulkarni Avatar answered Sep 16 '22 11:09

Amol M Kulkarni