Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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