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 Loops, Arrays and Objects Simplify Repetitive Tasks with Loops For Loops

Tobi Ogunnaike
Tobi Ogunnaike
2,242 Points

Why is the <div> written as a string i.e. '<div>'? I

As a beginner, I'm not quite sure why Dave considered the <div> as a string.. I understand that Dave set the html variable as an empty string because he ultimately wanted to add strings to the html variable. But it just seems a bit weird that <div> is a string, like other words. Do I just accept this or is there a deeper meaning e.g. this is the way to incorporate html 'functions' (if I can call div a function) into JavaScript.

Thanks!

Markus Ylisiurunen
Markus Ylisiurunen
15,034 Points

I might be able to help you but I can't see the video since I've paused my membership. So could you add some code here and I'll try my best to explain it to you.

2 Answers

Alan Winfrey
Alan Winfrey
2,953 Points

It is a string at the JavaScript layer. In the HTML layer it is an element tag with meaning to the browser renderer. document.write() outputs to the HTML renderer which interprets it for its output, your screen. You can do the same with most HTML tags;

Sean T. Unwin
Sean T. Unwin
28,690 Points

Hi Tobi,

Excellent question and I can understand your confusion with regards to this.

The reason it is a String is because document.write is a function which, as the name sounds, writes content to the document and this function takes a String as a parameter. So while this is a String, you are able to write (create) HTML elements with it, or really anything that can be considered to be a String. The JavaScript Interpreter within the browser will read the less-than and greater-than brackets and, in simple terms, attempt to create HTML tags out of them.

I hope that helps. :)