Using backslash to output string "\n"

" ‘\n’ " in js outputs ‘\n’. Does anyone know why that is not the case in observable or how I would replicate a similar output?

I imagine you mean inside a md literal string. You might try to add a backslash before:

md`\\n`

renders “\n”

Can you elaborate a bit, or maybe link to your notebook? I don’t quite understand what behavior you are looking for.

Thanks @severo , this helps!

@jrus, I am going through Eloquent JS currently and chapter 1 uses “”\n"" as an example to show how 2 backslashes collapse each other.

However when simply inputting “”\n"" in an observable cell returns "\\n" compared to “”\n"" in a js console.

As Severo mentioned, the solution is to use md. I still wonder why there is a difference though.

Aha, gotcha.

The output is just the exact string \n (i.e. a backslash followed by the letter n), but it is getting printed out as a Javascript literal, of a kind that you could copy/paste (with the surrounding quotation marks) into your code.

If you put html`\\n` as the source for a cell, you’ll get just the text \n in the cell output, as the content of the html element for the cell.

You can think of this as like the difference between:

console.log(JSON.stringify("\\n"))

vs.

console.log("\\n")
3 Likes