Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print new line in javascript

<!DOCTYPE html>
<html>

<head>
    <title>multiplication table</title>
</head>

<body>
    <script type="text/javascript">
    var i, j;
    var m = 1;

    for (i = 1; i <= 10; i++) {
        for (j = 1; j <= 10; j++) {
            document.write(i * j);
        }
        document.write("\n");
    }
    </script>
</body>

</html>

Output:

12345678910 2468101214161820 36912151821242730 481216202428323640 5101520253035404550 6121824303642485460 7142128354249566370 8162432404856647280 9182736455463728190 102030405060708090100

it gives output without printing the new line. I want to execute this without using

like image 982
Poushali Avatar asked Jan 20 '26 15:01

Poushali


1 Answers

Use <br /> to print text on new line. When you are putting the content in DOM. \n will just add a space instead of newline.

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

Example:

var i, j;
var m = 1;

for (i = 1; i <= 10; i++) {
  for (j = 1; j <= 10; j++) {
    document.write(i * j);
  }
  document.write("<br />");
}
like image 146
Tushar Avatar answered Jan 23 '26 05:01

Tushar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!