How do you end a 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(?);

}

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

How do you end a line in javascript?

Tom Gullen

60.4k82 gold badges277 silver badges448 bronze badges

asked Apr 22, 2011 at 17:29

2

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

answered Apr 22, 2011 at 17:31

Tom GullenTom Gullen

60.4k82 gold badges277 silver badges448 bronze badges

2

how about:

document.write ("<br>");

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

answered Apr 22, 2011 at 17:32

robrob

9,7576 gold badges41 silver badges71 bronze badges

0

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

document.write("<br>");

Here's a sample fiddle

  • http://jsfiddle.net/g6eAF/

answered Apr 22, 2011 at 17:33

JaredParJaredPar

712k146 gold badges1215 silver badges1441 bronze badges

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.

answered Apr 22, 2011 at 17:31

How do you end a line in javascript?

Jared FarrishJared Farrish

47.5k17 gold badges93 silver badges101 bronze badges

6

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

answered Apr 22, 2011 at 17:31

acconradacconrad

3,1931 gold badge21 silver badges31 bronze badges

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

answered Apr 22, 2011 at 17:36

How do you end a line in javascript?

alexalex

466k197 gold badges865 silver badges975 bronze badges

1

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,80814 gold badges57 silver badges108 bronze badges

answered Dec 22, 2016 at 8:29

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

answered Apr 22, 2011 at 17:31

kemiller2002kemiller2002

112k27 gold badges193 silver badges246 bronze badges

6

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.5k4 gold badges59 silver badges80 bronze badges

answered Sep 25, 2014 at 0:58

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

answered Sep 16, 2017 at 7:49

How do you end a line in javascript?

VicXjVicXj

1371 silver badge6 bronze badges

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

answered Dec 7, 2019 at 20:44

How do you end a line in javascript?

Shadab AliShadab Ali

1871 silver badge8 bronze badges

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

answered Apr 26, 2018 at 20:38

How do you end a line in javascript?

Try to write your code between the HTML pre tag.

answered Apr 4, 2019 at 10:48

How do you end a line in javascript?

1

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

answered Sep 19, 2021 at 10:46

How do you end a line in javascript?

RaLeRaLe

819 bronze badges

\n dosen't work. Use html tags

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

Dhia

9,22911 gold badges57 silver badges67 bronze badges

answered May 31, 2016 at 6:11

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

answered Jan 9, 2017 at 21:49

How do you end a line in 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 :)

answered Mar 15, 2017 at 9:52

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

}

answered May 16, 2019 at 6:58

How do you end a line in javascript?

2

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

answered Jul 29, 2021 at 16:21

How do you end a line in 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.

Does JavaScript need at end of line?

Answer 52b8cb427c82ca297c000836. In most cases, yes. JavaScript is an interpreted language, meaning it is parsed each time through and interpreted line by line, or statement by statement. The semi-colon signals JS of an 'end of statement' whereupon it quits parsing and executes it.

How do you break a line in node JS?

To create a line break in JavaScript, use “<br>”.

How do you break a line in typescript?

To add a new line in string in typescript, use new line \n with the + operator it will concatenate the string and return a new string with a new line separator. put the "\n" where you want to break the line and concatenate another string.