Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: How to print the raw content of a string including carriage returns?

For example if I have a variable:

const template = `line 1
line 2
line 3`;

I want console.log(template) to print:

line 1\nline 2\nline 3

like image 284
sonrh Avatar asked Oct 25 '25 03:10

sonrh


1 Answers

Just stringify it

const template = `line 1
line 2
line 3`;

console.log(JSON.stringify(template))
like image 81
epascarello Avatar answered Oct 26 '25 18:10

epascarello