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, ...)
Call
What 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
Parameter
Description
markup...
Required. One or more strings / TrustedHTML chunks (MDN)
Return Value
Return Value
Description
*
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);
Another writeln.D\n starts on the next line inside <pre>.
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);
Create a node.createElement("pre") builds the container.
Set text safely.textContent does not parse HTML — better XSS posture.
Insert once.replaceChildren / append updates the live tree.
Prefer this. MDN steers new work to DOM APIs, not writeln.
Compatibility
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.
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.