How do I create a new line in Javascript?
var i;
for(i=10; i>=0; i= i-1){
var s;
for(s=0; s<i; s = s+1){
document.write("*");
}
//i want this to print a new line
/document.write(?);
}
I am printing a pyramid of stars, I can't get the new line to print.
Solution 1:
Use the \n
for a newline character.
document.write("\n");
You can also have more than one:
document.write("\n\n\n"); // 3 new lines! My oh my!
However, if this is rendering to HTML, you will want to use the HTML tag for a newline:
document.write("<br>");
The string Hello\n\nTest
in your source will look like this:
Hello!
Test
The string Hello<br><br>Test
will look like this in HTML source:
Hello<br><br>Test
The HTML one will render as line breaks for the person viewing the page, the \n
just drops the text to the next line in the source (if it's on an HTML page).
Solution 2:
how about:
document.write ("<br>");
(assuming you are in an html page, since a line feed alone will only show as a space)
Solution 3:
Use a <br>
tag to create a line break in the document
document.write("<br>");
Here's a sample fiddle
- http://jsfiddle.net/g6eAF/