How do I create a new line in Javascript?

JavascriptHtmlNewlineLine Breaks

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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 - Javascript

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

document.write("<br>");

Here's a sample fiddle

Solution 4 - 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.

Solution 5 - Javascript

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

Solution 6 - Javascript

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

Solution 7 - Javascript

In html page:

document.write("<br>"); 

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

document.write("\n");

Solution 8 - Javascript

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

Solution 9 - Javascript

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

Solution 10 - Javascript

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);
 }

Solution 11 - Javascript

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

Solution 12 - Javascript

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>");
}

Solution 13 - Javascript

Try to write your code between the HTML pre tag.

Solution 14 - Javascript

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

Solution 15 - Javascript

\n dosen't work. Use html tags

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

Solution 16 - Javascript

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/>");.

Solution 17 - Javascript

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

Solution 18 - Javascript

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>');

}

Solution 19 - Javascript

if the program is integrated into html use; document.write("
");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTechnupeView Question on Stackoverflow
Solution 1 - JavascriptTom GullenView Answer on Stackoverflow
Solution 2 - JavascriptrobView Answer on Stackoverflow
Solution 3 - JavascriptJaredParView Answer on Stackoverflow
Solution 4 - JavascriptJared FarrishView Answer on Stackoverflow
Solution 5 - JavascriptacconradView Answer on Stackoverflow
Solution 6 - JavascriptalexView Answer on Stackoverflow
Solution 7 - JavascriptSuhani MendaparaView Answer on Stackoverflow
Solution 8 - Javascriptkemiller2002View Answer on Stackoverflow
Solution 9 - JavascriptTito E SurekView Answer on Stackoverflow
Solution 10 - JavascriptShadab AliView Answer on Stackoverflow
Solution 11 - JavascriptVicXjView Answer on Stackoverflow
Solution 12 - JavascriptAdeojo Emmanuel IMMView Answer on Stackoverflow
Solution 13 - JavascriptMacario PoloView Answer on Stackoverflow
Solution 14 - JavascriptRaLeView Answer on Stackoverflow
Solution 15 - JavascriptParadox NafiView Answer on Stackoverflow
Solution 16 - JavascriptA Coding PandaView Answer on Stackoverflow
Solution 17 - Javascriptuser2768308View Answer on Stackoverflow
Solution 18 - JavascriptNagnath MungadeView Answer on Stackoverflow
Solution 19 - JavascriptEdith StarkView Answer on Stackoverflow