Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,083 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
216,083 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.