The writeln() method

The writeln() method works just like the document.write() method but adds a JavaScript newline character (\n) at the end of the statement.

JavaScript newline character places a new line ONLY in the source code when using document.write() method to make it easier to read.
However, it can be use to create new lines in display elements such as alert boxes, prompts, etc.

document.write("Hello world! \n Is everything okay?");

Since the document.writeln() method adds a new line character at the end of the statement we could rewrite the code as follow:

document.writeln("Hello world!");
document.writeln("Is everything okay?");

Although the code is on two lines, the appearance in the browser is the same.

Leave a Reply