Wei WU
Wei WU

Reputation: 47

what's the different between <br> and \n as line break

I just finished a very simple assignment from my class. The part of the wrong code of script I wrote is like this:

alert("The circumference of a circle with this radius is " + circum + 
    "<br>" + "The area of a circle with this radius is " + area + "\n" + 
    "The surface area of a sphere with this radius is " + surarea + "\n" + 
    "The volume of a sphere with this radius is " + volume + "\n");

I have tried both <br> and \n in this part of code, and I am pretty sure that other parts are all right since I have tested them.

I was just wondering why the <br> doesn't work for me on my program. The example that the teacher gave us seems working fine. But he also told us that <br> should be used in HTML while \n is used in JS.

Thank you very much.

Upvotes: 4

Views: 9934

Answers (4)

Khaibar
Khaibar

Reputation: 149

<br> and <br /> are HTML and XHTML tags, while \n is a newline in code.

Upvotes: 1

ayushgp
ayushgp

Reputation: 5101

\n is a linebreak character. It is interpreted like that by the JS compiler. The HTML parsers on the other hand treat it as a normal string.

<br> in html is a tag to introduce a line break. In JS strings, this is treated as a normal string.

Upvotes: 4

Muhamamad Adil
Muhamamad Adil

Reputation: 41

The <br> or <br /> tag is an HTML element that will display everything after this <br> or <br /> by starting from new line when rendered in browser while the \n is used to jump to next line in the source code or the output prompt in standard output.

Upvotes: 1

Jalees Ahmad
Jalees Ahmad

Reputation: 80

\n is a new line feed within the context of plain text while <br> is line break within the context of HTML

Upvotes: 2

Related Questions