Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Build a Random Number Guessing Game

Michelle Gonzalez
Michelle Gonzalez
4,205 Points

Why is the <p> tag used in JavaScript?

I know it is used often in HTML coding to define a paragraph but i am not sure as to why it is necessary in JavaScript or the reasoning behind it?

5 Answers

Sharina Jones
PLUS
Sharina Jones
Courses Plus Student 18,771 Points

Hi Michelle,

JavaScript allows you to write HTML to the page. When you use the <p> tag in your JavaScript code, you're tell the interpreter that you want to add a paragraph to the page.

For example :

<body>
<script>
  document.write("<p>Without this code, your body tag would be empty. This paragraph is created when the browser gets to the script tag</p>");
</script>
</body>

If you removed the script tag from the above code, you would just have an empty body element. The <p>aragraph isn't a part of the body element. It is being added to the body element by the JavaScript code.

Does that make sense?

Jonathan Rosero
Jonathan Rosero
1,903 Points

Believe it or not some of this stuff works in OfficeTrax when adding notes!!

Michelle Gonzalez
Michelle Gonzalez
4,205 Points

Haha! Oh I know! I've used the <br> tag in OT. There's other tags we can use too though??

I was wondering the same thing. I realized when using document.write using <p> was necessary but when using the alert function you just need to use quotes.

Zachary Logan
Zachary Logan
3,001 Points

Usually the <p> tag in javascript is used when adding code to your HTML via JavaScript. This would be smart in a scenario when you wan something happening in the Javascript to affect the HTML and change what the page says! Hope this helps!

I think it's in this case a "Style" thing... when you dont use the <p> Tag it goes with the design of the h1 tag.