HTML DOM Document writeln() method

Definition and usage

writeln() This method writes directly to the opened (HTML) document stream.

writeln() This method is the same as the write() method, but adds a newline character after each statement.

Warning

writeln() This method deletes all existing HTML when used on a loaded document.

writeln() This method cannot be used with XHTML or XML.

See also:

Document write() method

Document open() method

Document close() method

Example

document.writeln("Hello World!");
document.writeln("Have a nice day!");

Try it yourself

Syntax

document.writeln(exp1, exp2, exp3, ...)

Parameter

Parameter Description
exp1, exp2, exp3,
...

Optional. Output stream.

Allows multiple parameters and appends them to the document in the order they appear.

Return value

None.

Difference between write() and writeln()

writeln() adds a newline character after each statement. write() does not.

Example

document.write("Hello World!");
document.write("Have a nice day!");
document.write("<br>");
document.writeln("Hello World!");
document.writeln("Have a nice day!");

Try it yourself

Note

Use in HTML writeln() is meaningless. It is only useful when writing a text document (type=".txt"). Newline characters are ignored in HTML.

If you want to break lines in HTML, you must useParagraphOr <br>

Example 1

document.write("Hello World!");
document.write("<br>");
document.write("Have a nice day!");

Try it yourself

Example 2

document.write("<p>Hello World!</p>");
document.write("<p>Have a nice day!</p>");

Try it yourself

Browser support

All browsers support document.write

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support