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 Useful Array Methods

Ali Amirazizi
Ali Amirazizi
16,087 Points

What is the difference between the print() and document.write() ?

I don't understand why the the print method was used instead of document.write(). Can they be used interchangeably?

Jared Bothwell
Jared Bothwell
7,031 Points

Thanks - just had the same question. I was wondering why use print() and not document.write.

4 Answers

Ali Amirazizi
Ali Amirazizi
16,087 Points

Thanks Ken. Also I made mistake. I thought the the print function being used in this video was the build in JavaScript print() method. I realized Dave made his own print function, which uses the document.write().

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Ali;

I have always thought that the document.write() method should be used only for testing. Here is a post on this issue that goes into some detail on the document.write() method.

Happy coding,

Ken

Hey Ken,

In your opinion, is this still true or is a better option available today?

Thanks,

Suresh

Isaac Parham
Isaac Parham
9,496 Points

Ken Alger,

I am confused by this exchange after reading your linked article and searching the MDN documentation. Should we not be using the document.write() method to write html to the page? If not, what is the alternative "built[sic] in JavaScript print() method" mentioned by Ali above? I cannot find any reference to it in the MDN documentation.

Thank you!

~Isaac

Robert Mews
Robert Mews
11,540 Points

Hey Ken,

From what I understand, document.write() is the standard function to write text to the DOM (i.e. browser). However, Dave is using print() because it calls the function in which document.write() appears. The reason behind this is the DRY methodology. By placing document.write() into a function, now we don't have to repeat the code that appears in that function, we simply call that function by using print(). Hope that makes sense.

Cesare Parmiggiani
Cesare Parmiggiani
8,017 Points

I tried to use print instead document.write in order ta "call" arrays inside a var, but nothing appears on screen, on the other side if I use document.write everything works fine.

print() only works with functions?

Cesare,

You must actually create the print() function definition, containing the document.write() function, and include the definition (or a link to it) in your project(s).