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) Storing and Tracking Information with Variables Strings and Numbers

HTML tags that are apart of string literals

For the sake of understanding document.write(), why are tags included within the quotes? Why is it that the syntax cannot be

document.write(<h1>"Title"</h1>);

3 Answers

Steven Parker
Steven Parker
231,275 Points

The document.write function takes a single string argument with tags included.

If you move the tags outside of the quotes, they are no longer part of the string and are interpreted as JavaScript code. The angles will be considered as inequality comparison operators ("<" = "less than", ">" = "greater than"), the slash ("/") is a numeric divide operator, and "h1" will be treated as an undefined variable. Of course, this will all cause a syntax error.

Because you are mixing two types of code here. Everything outside of the quotes is your javascript , and as both of your "h1" tags our outside of your quotes , and as it is not defined within javascript , it will cause a syntax error.

Because you're putting everything within the quotation into the HTML document (usually you would select a specific id from the html file).

H1 isn't a javascript element, it is an html element. You are literally putting html into an html document when using document.write.