+ 1
How do I make a newline in a javascript string
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.
+ 1
You can achieve it using "\n" literal. For example:
https://code.sololearn.com/WQVGSsonPQ5y/?ref=app
+ 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
...
+ 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
+ 1
Thank you. So could I also do document.write("<br>") ?
+ 1
Yes, of course.
0
Is "string" the same as 'string'
0
String can be declared either using single ('string') or double ("string") quotes.