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
Saidusmon Orip
379 Pointsdid I miss anything ;/ ?
did I miss anything? Why in document.write section inside brackets always comes <h> from HTML? Can't we just put the text we want to be printed inside the strings?
1 Answer
Michael Cook
Full Stack JavaScript Techdegree Graduate 28,975 PointsHey Saidusmon, the reason why you would use HTML tags inside the document.write() method is to write some piece of HTML to the document. You could also write plain text to the document, but there are many cases where if you do this, it won't actually resolve properly in the web browser. This is because HTML is supposed to "mark up" the text you want to put into a web page so that the browser will display it correctly.
Take for instance the sentence "Saidusmon's Website". You could add this to an HTML document using JavaScript by typing document.write('Saidusmon\'s Website'); and this would put the text in the page. However, if you wanted it to be formatted as the header for your page, it would be much better to type document.write('<h1>Saidusmon\'s Website</h1>');. In this case, you are adding an h1 element with the text you want to add already in it, and the browser's user agent stylesheet along with any other CSS you have written will format that h1 in a more appropriate way.
However, I wouldn't worry too much. As you continue to learn, you are going to find that there are better and more modern ways of adding content to a web page than the document.write() method. Just keep on progressing through the material and whenever you want to tinker with some code to see how it works, press CRTL + SHIFT + J in your Chrome browser and run your code in the Chrome console. This is a method I use pretty much every day to see how pieces of code will work, and I learn a lot by doing it. Good luck!
Note: I add a \ character inside "Saidusmon's Website" just before the apostrophe like this: "Saidusmon\'s Website". Using \ before a character like an apostrophe allows that character to properly resolve in a string. If you want to understand more, read about escape characters at https://javascript.info/regexp-escaping
Matthew Griffith
21,003 PointsMatthew Griffith
21,003 PointsYou should be able to only have strings (as long as they are in quotes).