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

Jonathan Franklin
Jonathan Franklin
3,883 Points

document.write()?

This is probably a stupid question, but what is the purpose of document.write()? I just started javascript basics so I don't know anything about it really.

1 Answer

Steven Parker
Steven Parker
229,786 Points

The document.write() method writes strings into your HTML page.

It's mainly used for testing or writing contents into an otherwise blank page. As you progress through the courses, you will learn other and more commonly used ways of adding and modifying page content via the DOM (Document Object Model).

Jonathan Franklin
Jonathan Franklin
3,883 Points

Thank you for replying! So this isn't the same as writing something in the html? I guess what I'm trying to figure out is what the difference between document.write (besides testing) and writing in the html.

Steven Parker
Steven Parker
229,786 Points

If I understand your question it might be the same thing. The document.write() function is method a JavaScript program can use to write into the HTML "document". What is written can be a mix of text and markup. It would have the same effect if you put the contents of the function argument into an HTML file and opened it with your browser.