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

Hunter Meo
Hunter Meo
4,817 Points

Why do we add the following in quotes? <p>

I was wondering why the paragraph code is added in quotes to the code.

3 Answers

I assume you're talking about this piece of code:

document.write('<p>You guessed the number</p>');

In here, we call a "write" method of the "document" object. The "write" method is a function taking 1 argument which is a string of text that you're telling JavaScript to write to the page. JavaScript then writes the following string of text which is seen by HTML as a valid markup because of the <p> tags residing in the string.

The quotations are here, because that's how JavaScript knows that what you're passing it is a string.

If you simply put <p>You guessed the number</p> (without the quotes) as an argument, JavaScript wont be able to recognize what it is, because it cannot directly read HTML markup and will see it as a Syntax Error.

I thought it was quite simply to insert a paragraph element in order to be more specific with inserting the string on to the page. But I'm still learning so there you go.

because you can give a style to you <p> with css