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 Basics (Retired) Introducing JavaScript Your First JavaScript Program

Lauren Oldenburg
Lauren Oldenburg
6,187 Points

What is the benefit of using document.write(""); when I could just type my text in HTML?

I don't understand what the benefit of this function is. It seems like more work to type all that out as opposed to just typing plain HTML such as <h1>Hello world</h1>. What am I missing?

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Lauren Oldenburg ! As you continue down this path, you will find that a lot of front-end JavaScript is about reacting to what your users do. And users are wildly unpredictable. There will be times when you won't know in advance what should be on the page.

Take this example: we're building a small addition app to quiz children for addition from 1+1 up to 20+20. So there are a number of possible questions. It would be considerably less work to dynamically generate those questions and write them on the fly to the same page than it would be to create a page for each question. Then if you also have to tell them if they got it right or wrong, you would want to dynamically add that message as the event happens.

As you progress, you will discover ways to write HTML to specific parts of the page instead of just replacing the entire body. This is an example to show you the power that JavaScript has to alter the page you are currently looking at :smiley:

Hope this helps! :sparkles:

Lauren Oldenburg
Lauren Oldenburg
6,187 Points

Hi Jennifer, Thank you so much for your detailed response and for explaining that so well! Makes sense now :)