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 trialBenjamin Hedgepeth
5,672 PointsHTML 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
231,269 PointsThe 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.
James Draper
1,172 PointsBecause 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.
Randy Eichelberger
1,491 PointsBecause 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.
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsAakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsThanks Steven . It helped me a lot as a beginner . Explained Beautifully :)