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

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
Technupe
  • 4,831
  • 14
  • 34
  • 37

18 Answers18

262

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

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
  • 9
    By definition, if the OP is using `document.write`, it's an HTML page, not an XHTML page. `
    ` is the correct linebreak for an HTML page. `
    ` is XHTML.
    – T.J. Crowder Apr 22 '11 at 17:38
  • 1
    thanks for the ``` document.write("
    ");``` detail. i was about to get crazy
    – BerkGarip Mar 11 '21 at 08:24
34

how about:

document.write ("<br>");

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

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
rob
  • 9,933
  • 7
  • 42
  • 73
14

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

document.write("<br>");

Here's a sample fiddle

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    +1 as this seems to be what the OP is *really* looking for :) (br vs \n) – Demian Brecht Apr 22 '11 at 17:34
  • How do you know? Good to point out BR, but a PRE could have just as easily been in there too. If they are building ASCII art, my thought is it's plaintext. – Jared Farrish Apr 22 '11 at 17:37
  • @Jared: How do you call `document.write` in a plain text document? (E.g., how do you put in the `script` tag?) Good point about the `pre`, though, could easily be a `pre` section. – T.J. Crowder Apr 22 '11 at 17:40
  • @T.J. - The display is plaintext, as in ASCII art. It's still an HTML document, although, it's just showing it as plaintext display. – Jared Farrish Apr 22 '11 at 17:42
  • @Jared: Yeah, I took your point. (I thought your other bit was about a `text/plain` doc, but I think I was clear about the `pre` being a good point.) – T.J. Crowder Apr 22 '11 at 17:45
7

Alternatively, write to an element with the CSS white-space: pre and use \n for newline character.

alex
  • 479,566
  • 201
  • 878
  • 984
  • 1
    Thanks! That was close, and `white-space: pre-wrap` did exactly what I wanted! https://www.w3schools.com/cssref/pr_text_white-space.asp – frederj Sep 17 '19 at 21:14
  • This is the only thing that worked for me. My app has a div, that for some reason, was taking the \n as a whitespace and NOT as a new line. – Dan Nov 27 '22 at 01:32
5

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.

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
4

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

acconrad
  • 3,201
  • 1
  • 22
  • 31
3

In html page:

document.write("<br>"); 

but if you are in JavaScript file, then this will work as new line:

document.write("\n");
Sid
  • 4,893
  • 14
  • 55
  • 110
Suhani Mendapara
  • 297
  • 1
  • 3
  • 10
2

For a string I just write "\n" to give me a new line. For example, typing console.log("First Name: Rex" + "\n" + "Last Name: Blythe"); Will type:

First Name: Rex

Last Name: Blythe

talha2k
  • 24,937
  • 4
  • 62
  • 81
2

To create a new line, symbol is '\n'

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('\n');

}

If you are outputting to the page, you'll want to use "<br/>" instead of '/n';

Escape characters in JavaScript

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
1

\n --> newline character is not working for inserting a new line.

    str="Hello!!";
    document.write(str);
    document.write("\n");
    document.write(str);

But if we use below code then it works fine and it gives new line.

    document.write(str);
    document.write("<br>");
    document.write(str);

Note:: I tried in Visual Studio Code.

VicXj
  • 165
  • 1
  • 6
  • This assumes that the text will be displayed as HTML. Not sufe if that is really a generic answer here. – GhostCat Sep 16 '17 at 08:14
1

you can also pyramid of stars like this

for (var i = 5; i >= 1; i--) {
     var py = "";
     for (var j = i; j >= 1; j--) {
         py += j;

     }
     console.log(py);
 }
Shadab Ali
  • 369
  • 3
  • 10
0

\n dosen't work. Use html tags

document.write("<br>");
document.write("?");
Dhia
  • 10,119
  • 11
  • 58
  • 69
0

Try to write your code between the HTML pre tag.

0

It's pretty easy actually. :D

To make a new line, you only need to use \n "function". For HTML related-projects, use only <br>, like in actual HTML script. :)

var i;
for(i=10; i>=0; i= i-1){
   var s;
   for(s=0; s<i; s = s+1){
    document.write("*");
   }
   // Here's the change
   document.write('\n');

}

THE OUTPUT

*
*
*
*
*
*
*
*
*
*
*

BUT, be careful the output above won't work in some HTML related projects. For that, you need to use <br> - like in HTML :D

RaLe
  • 99
  • 9
-1

If you are using a JavaScript file (.js) then use document.write("\n");. If you are in a html file (.html or . htm) then use document.write("<br/>");.

-1
document.write("\n");

won't work if you're executing it (document.write();) multiple times.

I'll suggest you should go for:

document.write("<br>");

P.S I know people have stated this answer above but didn't find the difference anywhere so :)

-1

your solution is

var i;
for(i=10; i>=0; i= i-1){
   var s;
   for(s=0; s<i; s = s+1){
    document.write("*");
   }
   //printing new line
   document.write("<br>");
}
Adeojo Emmanuel IMM
  • 2,104
  • 1
  • 19
  • 28
-2

You can use below link: 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('<br>');

}
  • 2
    Your link is broken, and your code example says nothing that the accepted answer from 2011 doesn't already say. – Quentin May 16 '19 at 07:02
  • Well, the link is fixed, but it points to an explanation-free demo and you still aren't saying anything now already said in 2011. – Quentin May 17 '19 at 13:25