JavaScript JavaScript Basics Introducing JavaScript Your First JavaScript Program

Lauren Oldenburg
Lauren Oldenburg
668 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
STAFF
Jennifer Nordell
Treehouse Staff

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
668 Points

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