\n doesn't work | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

\n doesn't work

I am running this code, but it outputs everything on the same line. Why \n doesn't work here? document.write ('Line 1\n Line 2');

22nd Oct 2018, 2:28 PM
Vectravox
Vectravox - avatar
8 Answers
+ 12
Yeah. IIRC, HTML line breaks are done via <br/>. You may use those instead, since that's how HTML documents are parsed.
22nd Oct 2018, 2:33 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Thanks! With <br/> it works fine!
22nd Oct 2018, 2:40 PM
Vectravox
Vectravox - avatar
+ 3
[meta] <pre> tags don't "interfere" with text so literal carriage returns are okay there. Hatsy Rei has the right context for what you're doing.
22nd Oct 2018, 5:12 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
PGameYT no... you use \n in javascript too... like alert: alert("line 1 \n line2")
24th Oct 2018, 12:10 AM
Jingga Sona
Jingga Sona - avatar
+ 3
You can use \n only in alert and console methods.
24th Oct 2018, 7:54 AM
Seniru
Seniru - avatar
0
simply use <br> in code document.write('Line 1 <br> Line2');
23rd Oct 2018, 6:40 AM
Fahad Ahmad
Fahad Ahmad - avatar
0
oh I know the console stuff but I nevr knew you could do that in J.S. Thats cool thanks for the advice. 👍👍👍
26th Oct 2018, 10:25 PM
PGameYT
PGameYT - avatar
- 2
\n is only us ed in C
23rd Oct 2018, 11:35 PM
PGameYT
PGameYT - avatar