How do I create a new line in Javascript?

Use "\n":

document.write("\n");

Note, it has to be surrounded in double quotes for it to be interpreted as a newline. No it doesn't.


Use a <br> tag to create a line break in the document

document.write("<br>");

Here's a sample fiddle

  • http://jsfiddle.net/g6eAF/

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


how about:

document.write ("<br>");

(assuming you are in an html page, since a line feed alone will only show as a space)