JavaScript Document writeln() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

document.writeln(...) writes one or more strings (or TrustedHTML) into a document stream opened by document.open(), then adds a newline (MDN). It is essentially document.write() plus \n.

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

Syntax

Call document.writeln. Start with this line.

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

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

Library declarations
JavaScript
document.writeln(markup, ...)

Parameters

ParameterDescription
markup...Required. One or more strings / TrustedHTML chunks (MDN)

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Stream into a <pre>

Classic literacy pattern: open, writeln lines, close (on an iframe document).

JavaScript
const doc = iframe.contentDocument;
doc.open();
doc.writeln("<pre>Hello world!");
doc.writeln("I am a fish");
doc.writeln("The number is 42</pre>");
doc.close();

console.log(doc.querySelector("pre").textContent);
Try It Yourself

How It Works

  1. Target an iframe doc. Keeps the parent page safe.
  2. Open a stream. open() starts (and clears) that document.
  3. Write with newlines. Each writeln appends a newline (MDN).
  4. Close the stream. close() finishes parsing so you can read the DOM.

Example 2 — writeln() vs write()

MDN: same API, but writeln adds a newline after the text.

JavaScript
const doc = iframe.contentDocument;
doc.open();
doc.write("<pre>");
doc.write("A");
doc.write("B");
doc.writeln("C");
doc.writeln("D");
doc.write("</pre>");
doc.close();

console.log(JSON.stringify(doc.querySelector("pre").textContent));
Try It Yourself

How It Works

  1. Use write for raw chunks. A then B stick together.
  2. Use writeln for a trailing newline. C becomes C\n.
  3. Another writeln. D\n starts on the next line inside <pre>.
  4. Rule. Need a visible break? Either writeln or put \n in write.

Example 3 — Prefer Modern DOM

Same multi-line text without a deprecated stream API.

JavaScript
const pre = document.createElement("pre");
pre.textContent = "Hello world!\nI am a fish\nThe number is 42";
host.replaceChildren(pre);

console.log(pre.textContent);
Try It Yourself

How It Works

  1. Create a node. createElement("pre") builds the container.
  2. Set text safely. textContent does not parse HTML — better XSS posture.
  3. Insert once. replaceChildren / append updates the live tree.
  4. Prefer this. MDN steers new work to DOM APIs, not writeln.

Deprecated Browser Support

Document.writeln() is Deprecated on MDN, but remains widely implemented for legacy compatibility. Logos use the shared browser-image-sprite.png sprite from this project.

✓ Deprecated · Legacy

Document.writeln()

Write-plus-newline document stream API. Literacy only — do not use in new products.

Legacy Deprecated API
Google Chrome Legacy
Legacy
Microsoft Edge Legacy
Legacy
Opera Legacy
Legacy
Mozilla Firefox Legacy
Legacy
Apple Safari Legacy
Legacy
Internet Explorer Legacy
Legacy
document.writeln() Do not use

Bottom line: Educational reference only. Prefer createElement / textContent / append. See the MDN Document.writeln() reference.

Frequently Asked Questions

MDN: Document.writeln() writes text in one or more TrustedHTML or string parameters to a document stream opened by document.open(), followed by a newline character.
MDN: writeln() is essentially the same as document.write() but adds a newline after the written text. That newline is only visible when injected inside an element that displays newlines (for example a pre).
Yes. MDN marks Document.writeln() as Deprecated. Prefer modern DOM APIs such as createElement, textContent, append, or carefully sanitized HTML insertion.
None (undefined) (MDN).
Yes. MDN: writeln() parses input as HTML and is an injection sink. Never pass untrusted user content. Prefer TrustedHTML when Trusted Types are enforced.
Do not call document.writeln() on the live tutorial page. Use an iframe’s contentDocument with open(), writeln(), and close(), as in the try-it labs.

Next: exitFullscreen()

Learn how document.exitFullscreen() leaves fullscreen presentation.

exitFullscreen() →

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.

12 people found this page helpful