Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a new line in Javascript?

People also ask

Does \n work in JavaScript?

"\n" does work. If you do a document.

How do I add a new line to a string?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

How do I create a new line?

To add spacing between lines or paragraphs of text in a cell, use a keyboard shortcut to add a new line. Click the location where you want to break the line. Press ALT+ENTER to insert the line break.


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)


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 "\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.


document.writeln() is what you are looking for or document.write('\n' + 'words') if you are looking for more granularity in when the new line is used