What is Document.write?
Document.write is a JavaScript command that allows you to write HTML directly into the page. It is a powerful tool that can be used to create dynamic web pages. It is important to note that document.write should only be used for small amounts of HTML, as it can cause problems with page loading and rendering.
How to Use Document.write
Using document.write is fairly straightforward. To use it, you simply need to include the command in your JavaScript code. For example, if you wanted to write a simple HTML paragraph, you could use the following code:
document.write('<p>This is a paragraph.</p>');
This code will write the HTML paragraph directly into the page. You can also use document.write to write more complex HTML, such as tables and lists. For example, the following code will write a simple table into the page:
document.write('<table><tr><th>Name</th><th>Age</th></tr><tr><td>John</td><td>25</td></tr></table>');
Document.write can also be used to write JavaScript variables into the page. For example, if you had a variable called “name” that contained the value “John”, you could use the following code to write it into the page:
document.write(name);
This code will write the value of the “name” variable into the page. It is important to note that document.write can only be used to write HTML and JavaScript variables into the page. It cannot be used to write other types of data, such as images or audio files.
Summary
Document.write is a powerful tool that can be used to create dynamic web pages. It is important to note that document.write should only be used for small amounts of HTML, as it can cause problems with page loading and rendering. With a little practice, you should be able to use document.write to create dynamic web pages with ease.


2 responses to “How to Use Document.write in JavaScript”
The warning about using document.write only for small amounts of HTML and the potential to break the page if used after load really stood out to me. I am curious how you would compare document.write with more modern approaches like manipulating the DOM via document.createElement or using innerHTML, especially in terms of performance and security. In practical, real-world projects, do you still see any legitimate use cases where document.write is actually the best or simplest option, or would you recommend phasing it out entirely for beginners learning JavaScript today?
Nina, I am glad that warning stood out to you because it is really the core of why I treat document.write as legacy. In practice, I would recommend beginners skip it and focus on DOM methods and innerHTML, which are both safer and easier to reason about; one extra nuance is that innerHTML can expose you to XSS if you inject untrusted strings, whereas createElement and textContent make it much harder to accidentally run malicious code. The only remaining niche where document.write sometimes appears is in very old third-party ad or analytics snippets, but even there I treat it as something to integrate cautiously rather than a pattern to learn from.