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

Tanawee Wonwaleepoj
Tanawee Wonwaleepoj
1,840 Points

<p> tag on document.write.

Why do you have to add <p> tag before the message with document.write? Can't I type the string in the parenthesis directly?

3 Answers

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

The p tag adds HTML formatting to the statement you right. You don't have to do it this way.

You can format the text on document.write by adding it as a src attribute.

<div id = "formattngForDocumentWrite">

    <script src="app.js">
    var paragraph = document.write("This is a message");

And style it with your CSS

#formattngForDocumentWrite {
    color: red;
    font-size: 1.3em;
Michael L
Michael L
2,482 Points

I had the same question... I think we also have written programs with document.write without using < p > and < / p > tags. I tried just removing those, and it still seems to work... the text looks smaller though... So what exactly will happen if we have this code as a part of a larger program and do not use the < p > tags?

Leandro Severino
Leandro Severino
12,674 Points

For testing code purposes, tags doesn't really matter if you just need to know the data. The main purpose of tags is to tell the browser how to display the content.

For me, I still add the paragraph tag because Inserting it starts the text on the next line making it more readable if you have multiple contents on a single page. You can check document.writeln() too.