user570098 user570098 - 2 years ago 115
Javascript Question

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){
//i want this to print a new line


I am printing a pyramid of stars, I can't get the new line to print.

Answer Source

Use the \n for a newline character.


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:


The string Hello\n\nTest in your source will look like this:



The string Hello<br><br>Test will look like this in HTML source:


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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download