简体   繁体   中英

How do I create a 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(?);

}

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

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

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()是您要查找的内容,或者document.write('\\n' + 'words')如果您正在寻找使用新行时的更多粒度

In html page:

document.write("<br>"); 

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

document.write("\n");

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

或者,使用 CSS white-space: pre写入元素并使用\\n作为换行符。

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

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

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

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

尝试在 HTML pre 标记之间编写代码。

\\n dosen't work. Use html tags

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

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

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

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

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM