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 trialAli Amirazizi
16,087 PointsWhat 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?
4 Answers
Ali Amirazizi
16,087 PointsThanks 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
Treehouse TeacherAli;
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
Suresh Kasipandy
Courses Plus Student 3,657 PointsHey Ken,
In your opinion, is this still true or is a better option available today?
Thanks,
Suresh
Isaac Parham
9,496 PointsI 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
11,540 PointsHey 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
8,017 PointsI 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?
Brent Wright
1,298 PointsCesare,
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).
Jared Bothwell
7,031 PointsJared Bothwell
7,031 PointsThanks - just had the same question. I was wondering why use print() and not document.write.