why doesn't javascript print new row with \n?

373 Views Asked by At

I have a problem with JS, whenever I write "\n" while in script tag, it doesn't print new row.I am new to Javascript so can someone explain to me what is the problem?

Here is the code from my excercise:

<html>
<head>
  <meta charset="utf-8">
  <script>
    var output;
    var number = window.prompt("Enter mobile no:  \n");
    if( number>10000000 && number<99999999){
     var ps = parseInt(number/1000000);
     var vs = parseInt((number/1000)%1000);
     var ts = number%1000;
     output = "0" + ps + "/" + vs + "-" + ts +" ";
      document.write("Mobile no. is : " + output + " "  + "\n");


   if( ps == 70 || ps == 71 || ps == 72 ){
      document.write("Mobile no. is T-Mobile " + "\n");
   }
   if( ps == 75 || ps == 76){
      document.write("Mobile no. is One " + "\n");
   }
   if( ps == 77 || ps == 78){
      document.write("Mobile no. is Vip " + "\n");
   }

    }
  </script>

</head>

<body>

//not important

</body>
</html>
3

There are 3 best solutions below

0
On

You're outputting to the browser, so HTML is relevant, not plain text. Just replace the "\n" with "<br>".

0
On

You are writing HTML to the page. It does not recognize new lines. You need to use the "<br>" tag.

0
On

If you are writing in a .html file use <br> tag like this:

document.write("<br>");

Otherwise, you can use "\n".