0

I am writing a very basic code in javascript and I'm looking forward to use "\n" for newline but it seems to not be working.

I have seen alternate solutions in other posts, but I wonder why is "\n" not working here.

Here's my code.

<html>

<body>
    <h2>welcome to java script</h2>
    <script>
        var x = 10;
        var y = 20;
        var sum = x + y;
        var mult = x * y;
        document.write("the sum is " + sum);
        document.write("\n");
        document.write(" the product is " + mult);
    </script>
</body>

</html>
ventaquil
  • 2,661
  • 3
  • 20
  • 45
conjuring
  • 121
  • 12

2 Answers2

1

The newline character (\n) breaks line within a string not in the document.

To break the line in the document you have to use the HTML <br/> element.

<html>

<body>
    <h2>welcome to java script</h2>
    <script>
        var x = 10;
        var y = 20;
        var sum = x + y;
        var mult = x * y;
        document.write("the sum is " + sum);
        document.write("<br/>");
        document.write(" the product is " + mult);
    </script>
</body>

</html>
Mamun
  • 62,450
  • 9
  • 45
  • 52
0

"\n" works in text files.You are writing to the DOM in HTML; the DOM recognizes

<br />

Use that instead.

Jackson Kamya
  • 518
  • 7
  • 6