How do I make a newline in a javascript string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do I make a newline in a javascript string

15th Dec 2018, 9:20 PM
zachiah
8 Answers
+ 5
Because in HTML, \n doesn’t mean anything. \n means newline is JS, but as we are writing it to the document, it is HTML. To yse a newline in HTML, use <br/>. So your code could be written as this: for (i=0;i<100;i++) { document.write(i); document.write("<br/>"); } and that will work.
16th Dec 2018, 4:19 AM
Rowsej
Rowsej - avatar
+ 1
You can achieve it using "\n" literal. For example: https://code.sololearn.com/WQVGSsonPQ5y/?ref=app
15th Dec 2018, 9:33 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Why does this: for (i=0;i<100;i++) { document.write(i); document.write('\n'); } Write 1 2 3 4 5 6... Not 1 2 3 4 5 6 7 ...
15th Dec 2018, 10:27 PM
zachiah
+ 1
Because HTML removes automatically white spaces and line breaks. You need a proper format in CSS and you'll see the result. Here's your code with included format in the CSS. https://code.sololearn.com/WMSY4fF0ES5a/?ref=app
15th Dec 2018, 10:55 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Thank you. So could I also do document.write("<br>") ?
15th Dec 2018, 10:56 PM
zachiah
+ 1
Yes, of course.
15th Dec 2018, 10:58 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
Is "string" the same as 'string'
15th Dec 2018, 9:42 PM
zachiah
0
String can be declared either using single ('string') or double ("string") quotes.
15th Dec 2018, 9:46 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar