JavaScript Document write() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.write(markup) writes HTML markup into a document stream opened by document.open(). It returns undefined. Learn it for literacy — do not use it in new apps.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call document.write. Start with this line.

JavaScript
document.write(markup, …)
CallWhat it does
document.write(markup, …)Primary signature.
document.write(...)Call with the arguments your case needs.

Built into the browser. Call write(...) on document — no import required.

Library declarations
JavaScript
document.write(markup, …)

Parameters

ParameterDescription
markup…Required. HTML string(s) or TrustedHTML

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Safe Stream into an Iframe

Write markup into an iframe document, then close the stream.

JavaScript
const iframe = document.getElementById("preview");
const doc = iframe.contentDocument;

doc.open();
doc.write("<p>Hello world!</p>");
doc.write("<p>I am a fish</p>");
doc.close();

console.log(doc.querySelectorAll("p").length); // 2
Try It Yourself

How It Works

  1. Sandbox. Use contentDocument so the host page is never cleared.
  2. Open. open() starts a fresh document stream.
  3. Write. Each string is parsed as HTML into that stream.
  4. Close. close() finishes parsing so the iframe shows the result.

Example 2 — Multiple Arguments

One write() call can accept several markup strings in order.

JavaScript
const doc = document.getElementById("preview").contentDocument;

doc.open();
doc.write(
  "<h1>Out with the old</h1>",
  "<p>in with the new!</p>"
);
doc.close();

console.log(doc.body.children.length); // 2
console.log(doc.querySelector("h1").textContent); // "Out with the old"
Try It Yourself

How It Works

  1. Open the stream. Same safe iframe pattern.
  2. Pass many strings. They are written in argument order.
  3. Parse as HTML. Tags become real DOM nodes in the iframe.
  4. Close. Completes the document so queries work.

Example 3 — Modern DOM Alternative

Build the same content with createElement — no document stream, no page wipe risk.

JavaScript
const host = document.createElement("div");

const h1 = document.createElement("h1");
h1.textContent = "Out with the old";

const p = document.createElement("p");
p.textContent = "in with the new!";

host.append(h1, p);
console.log(host.children.length); // 2
console.log(host.firstChild.textContent); // "Out with the old"
Try It Yourself

How It Works

  1. Create a host. A plain element holds the new nodes.
  2. Create children. createElement + textContent avoids HTML parsing surprises.
  3. Append. Nodes attach without opening a document stream.
  4. Why better. No auto-clear, clearer DOM, safer for untrusted text.

Browser Support

Document.write() still exists widely for compatibility, but MDN marks it Deprecated. Support ≠ recommended.

✓ Deprecated

Document.write()

Present in Chrome, Firefox, Safari, Edge, Opera, and Internet Explorer. Behavior after load and with async scripts varies — avoid relying on it.

Legacy Avoid for new work
Google Chrome Legacy support
Yes
Microsoft Edge Legacy support
Yes
Mozilla Firefox Legacy support
Yes
Apple Safari Legacy support
Yes
Opera Legacy support
Yes
Internet Explorer Legacy
Yes
write() Legacy / discouraged

Bottom line: Literacy and legacy maintenance only. See the MDN write() reference. Prefer createElement / append / replaceChildren for new code.

Frequently Asked Questions

It writes one or more HTML strings (or TrustedHTML objects) into a document stream opened by document.open(). Return value is undefined.
Yes. MDN marks Document.write() as Deprecated and strongly discourages using it because of idiosyncratic parser behavior that is hard to debug.
On a closed/loaded document, write() can automatically call document.open(), which clears the current page — then writes the new markup.
Yes. write() parses input as HTML and is an injection sink. Never pass untrusted user content. Prefer TrustedHTML when Trusted Types are enforced.
Prefer modern DOM APIs such as createElement, textContent, append, or replaceChildren. Build nodes instead of writing a document stream.
Do not call document.write() on a live tutorial page. Use an iframe’s contentDocument with open(), write(), and close().

Next: writeln()

Learn document.writeln() — write with a trailing newline (also deprecated).

writeln() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful