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,780
  • 3
  • 23
  • 48
conjuring
  • 121
  • 16

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
  • 66,969
  • 9
  • 47
  • 59
0

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

<br />

Use that instead.

jackthedev
  • 578
  • 5
  • 7