document.writeln() is a Deprecated instance method that writes one or more HTML strings (or TrustedHTML values) into a document stream opened by document.open(), then appends a newline (see MDN Document: writeln()). Learn how that newline only shows in whitespace-preserving elements, how it compares to write(), XSS / Trusted Types notes, safer alternatives, and five iframe-based try-it labs.
01
Kind
Instance method
02
Args
1+ markup strings
03
Returns
undefined
04
Extra
+ newline
05
Risk
XSS sink
06
Status
Deprecated
Fundamentals
Introduction
writeln() belongs to the same legacy document stream family as write():
MDN: the method is essentially the same as document.write() but adds a newline. That newline is only visible if it lands inside an element where newlines are displayed (MDN highlights <pre>).
⚠️
Never practice on the live tutorial page
Like write(), a late writeln() can auto-open() and wipe the current page. Labs use an iframe’s contentDocument so only the preview is replaced.
Examples follow MDN Document: writeln() and the related write/open/close stream. Prefer iframe demos.
📚 Getting Started
Write into an iframe document stream without touching the host page.
Example 1 — open → writeln → close
Build a small preview document with writeln into a <pre>.
JavaScript
const doc = document.getElementById("preview").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("pre text:", doc.querySelector("pre").textContent);
write block stays denser; writeln block shows extra breaks
How It Works
MDN’s key teaching point: writeln() equals write() plus a newline. Comparing both inside <pre> makes that difference concrete.
📈 Practical Patterns
MDN-style multi-call pre content, the after-load wipe trap, and a modern replacement.
Example 3 — Split strings across writeln calls (MDN style)
MDN writes heading and pre content across several writeln calls.
JavaScript
const doc = document.getElementById("preview").contentDocument;
const one = "<h1>Out with";
const two = "the old</h1>";
const three = "<pre>in with";
const four = "the new!</pre>";
doc.open();
doc.writeln(one);
doc.writeln(two, three);
doc.writeln(four);
doc.close();
iframe shows heading + pre; newlines appear inside the pre
How It Works
Arguments concatenate in order. MDN’s Trusted Types demo wraps each string with policy.createHTML(...) when enforcement is on.
Example 4 — After-load writeln can wipe a document
Demonstrate auto-open behavior on an iframe document only.
JavaScript
const doc = document.getElementById("preview").contentDocument;
doc.open();
doc.writeln("<p id='keep'>Original iframe content</p>");
doc.close();
document.getElementById("wipe").addEventListener("click", () => {
// Late writeln on a loaded document can open() and replace it.
doc.writeln("<p>Replaced via late writeln()</p>");
doc.close();
});
Original content disappears; only the late writeln remains
How It Works
Same trap as write(): after load, stream methods can clear the document. Keep experiments inside an iframe.
Example 5 — Prefer createElement + textContent
Multi-line text without a deprecated stream API.
JavaScript
const host = document.getElementById("host");
host.replaceChildren();
const pre = document.createElement("pre");
pre.textContent = "Out with the old\nin with the new!";
host.append(pre);
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 markup into a document stream, then a newline. Prefer modern DOM APIs for new code.
LegacyDeprecated
Google ChromeSupported (legacy)
Yes*
Mozilla FirefoxSupported (legacy)
Yes*
Apple SafariSupported (legacy)
Yes*
Microsoft EdgeSupported (legacy)
Yes*
OperaSupported (legacy)
Yes*
Internet ExplorerSupported (legacy)
Yes*
writeln()Avoid in new apps
Bottom line: Learn open/writeln/close for literacy. Demo newlines inside pre. Never inject untrusted HTML. Rebuild UI with createElement, textContent, and append instead.
Wrap Up
Conclusion
document.writeln() is a Deprecated stream writer that behaves like write() with an extra newline. Learn it for legacy literacy, demo the newline inside a <pre>, respect wipe and XSS risks, then build with modern DOM APIs.
Demo newlines inside <pre> when comparing to write()
Call open() → writeln() → close() as one unit
Migrate to createElement / textContent / append
❌ Don’t
Use writeln() in new products (MDN)
Call it on the live page after load
Pass untrusted user HTML strings
Expect newlines to show in normal paragraphs
Confuse document.writeln with console logging
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about writeln()
Deprecated write-plus-newline stream API.
5
Core concepts
📝01
Returns
undefined
MDN
📄02
vs write
+ newline
MDN
🗎03
Visible in
<pre>
whitespace
🛡04
Security
XSS sink
MDN
⚠️05
Status
Deprecated
MDN
❓ 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.
Did you know?
MDN notes that the newline from document.writeln() is only visible when it is injected inside an element where newlines are displayed. That is why demos usually write into a <pre> — in a normal paragraph the extra newline often collapses like ordinary HTML whitespace.