why chrome console not show "\n" as string but console.log output as normal line break?

Viewed 1509

Chrome Version: 90.0.4430.212

type "1\n2" then type "enter" showing "\n" string

type "console.log('1\n2')" then type "enter" showing normal

enter image description here

1 Answers

The console reads the \n as new lines and outputs it the way it would look with the line breaks.

If you want to see the \n in console use JSON.stringify().

Example:

 

console.log('1\n2');
console.log(JSON.stringify('1\n2'));

Related