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 Introducing Conditional Statements

Ryan Rassoli
Ryan Rassoli
3,365 Points

Using <p> tags in JavaScript

When do you use <p> tags in JavaScript. For instance, in one of the videos it says document.write ("<p>That's right!</p>"); but in other lines of the same program it doesn't include any tags. When do you put in the tags and what is the point if you already have the phrase in quotes?

4 Answers

Laly Singh
Laly Singh
2,029 Points

Hey Ryan!

document.write() and alert() are different methods. The alert box will just output a box on the browser, it will not write anything directly on the page, your HTML page. Try it, if you do alert(<p>This is right</p>), you will get an alert box with this "<p>This is right</p>" which is not really useful if you want your text to look like a paragraph right?

The document.write() will write something on the page. That's why you can use document.write(<p>This is right</p>) and document.write(This is right). It will display a different output.

First case document.write(<p>This is right</p>) it will write on your html "This is right" as a paragraph. Second case: document.write(This is right) it will write "This is right" as normal text.

Hope that clears things a little bit.

mc24
PLUS
mc24
Courses Plus Student 3,041 Points

Thats the same like you can use Javascript in HTML files with <script></script> .

You have to remember that most of the programming languages work together, even in the same file. There is no such thing like "you can only use HTML in HTML-Files".

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,723 Points

<p> tags and other tags are HTML. So if you wrap content with <p></p>, you're saying this is a paragraph element. Without a tag you are just placing text in the document.

Ryan Rassoli
Ryan Rassoli
3,365 Points

I understand that, but that's not what my question is asking. In the JavaScript file, there was a line of code that said alert("<p>This is right</p>") and the exact same thing again without the <p> tags. I don't understand when to use the p tags. I thought your only supposed to use those in HTML.

So using the <p></p> method in JavaScript is similar to writing inline CSS code?