I'm working on a project which deals with strings a lot. I need to show what kind of delimiter is used in strings. I get that value from an API call. How do I print the delimiter to the console?
delimiter = '\n';
I want to print this on screen.
console.log(delimiter);
// prints "
"
I need it to print \n
instead of literally printing newline.
I'm working on a project which deals with strings a lot. I need to show what kind of delimiter is used in strings. I get that value from an API call. How do I print the delimiter to the console?
delimiter = '\n';
I want to print this on screen.
console.log(delimiter);
// prints "
"
I need it to print \n
instead of literally printing newline.
-
1
delimiter = '\\n';
, perhaps? – esqew Commented Oct 8, 2021 at 10:47 - It is set from API call,Which I have no access to. – Prithvi Raj Commented Oct 8, 2021 at 10:57
3 Answers
Reset to default 4The character \n
is an escape sequence used for new lines.
That is why you need to use replace on the string if you want to see it in the console. Like so:
console.log(delimiter.replace('\n', '\\n'));
The extra backslash escapes the \n
so it is not longer treated as a newline.
More about escape sequences can be found here:
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#escape_sequences
To display a (multi)line with already wrapped text, use this