HTML DOM

HTML DOM writeln() Method

The writeln() method writes one or more strings to a document stream, exactly like write(), but adds a newline character after the output.


Definition and Usage

document.writeln() writes the given text to the document and appends a newline (\n) character. Because HTML collapses whitespace, the newline is usually invisible in rendered output - it only shows inside elements like <pre> that preserve whitespace.

Syntax

document.writeln(text1, text2, ..., textN)

It accepts one or more string arguments that are concatenated and written, then followed by a newline. It returns undefined.

Example

Newlines are preserved inside <pre>
<pre>
<script>
document.writeln("Line one");
document.writeln("Line two");
document.writeln("Line three");
</script>
</pre>

Inside the <pre> element, each writeln output appears on its own line because the newline it adds is preserved. Outside <pre>, the same code would render on a single line since HTML collapses whitespace.

⚠️

Like document.write(), calling writeln() after the page has finished loading overwrites the whole document. Prefer innerHTML or DOM creation methods in modern code.

Ready to use your HTML skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships