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 Tracking Multiple Items with Arrays Using For Loops with Arrays

Bonnie Martin
Bonnie Martin
13,671 Points

Why are we using print() instead of document.write()?

In the function there's a print(listHTML); that's printing the variable listHTML, which is being built up with the list items. Why is this not using document.write? What's the difference between document.write and print?

4 Answers

Josh Keenan
Josh Keenan
19,652 Points

If you look at the code, Dave has made a function called print() which writes to the document, that's all it is! It just saves you some time.

Richard Hriech
Richard Hriech
12,001 Points

Yes, the print function has only one line in it, but let's assume you use the function like 20 times in your script. What if you don't want to use document.write() anymore? You can change the function to use console.log() instead for example, and leave the rest of the code untouched.

Bonnie Martin
Bonnie Martin
13,671 Points

Ah-ha, thanks. So then I guess my next question is, why is that in a separate function? It only has one line in it. Just so that it could be used elsewhere, if some other part of the program wanted to print something?

Josh Keenan
Josh Keenan
19,652 Points

It's there to just save you a bit of time in the challenge and I think to give you a bit more experience of functions in JavaScript; and yes, it could be used elsewhere!