What is the DOM writeln() method?

Key takeaways:

  • writeln() writes content to an HTML document, adding a newline after each output.

  • It is similar to write(), but ensures, each piece of content appears on a newline.

  • It is outdated and not suitable for dynamic content or production use.

  • Modern alternatives like innerHTML, textContent, and createElement() offer more control.

  • Calling writeln() after the page has loaded can overwrite the document’s content.

  • It is supported by all modern browsers but is not recommended for production code.

In web development, the DOM (Document Object Model) is a crucial concept that represents the structure of HTML and XML documents as a tree of nodes. The DOM provides an interface for scripts to dynamically manipulate the content, structure, and style of web pages. It allows developers to interact with the elements of a web page programmatically using JavaScript.

One of the JavaScript methods that can be used to interact with the DOM is the writeln() method. This method is specifically used to write data to the document, similar to the write() method, but with a slight difference in functionality. Let’s explore the writeln() method in detail, including how it works, how it’s different from other writing methods, and its use cases.

What is the writeln() method?

The writeln() method is a part of the document object in JavaScript and is primarily used to write content into an HTML document, followed by a newline character. This newline character ensures that each output is written on a new line, which is particularly useful for generating content with clear separation between multiple pieces of data.

Syntax

The syntax of writeln() method is:

<pre>
<script>
document.writeln("exp");
</script>
</pre>
Syntax for using writeln().
  • <pre> tag is used to preserve white spaces and newlines.

  • <script> tag is used to write inline javascript code in an HTML file.

  • document is used to access the HTML document we want to change.

  • exp is the new text we want to add to the HTML document and is written inside the parenthesis within the quotation marks.

Note: We use writeln() method inside <pre></pre> tag as it preserves white spaces and newlines in HTML. Otherwise, HTML ignores the newline command.

Parameters

The writeln() method can have a varying number of parameters according to the requirement. When more than one parameter is sent to the method, it concatenates and writes them in the same line.

Method

Output

document.writeln("hello");

hello

document.writeln("hello" , "world");

helloworld

document.writeln("hello" , "world" , 5);

helloworld5

Example

Let’s write inside an HTML document using a writeln() method without the <pre> tag and then with the <pre> tag to see the difference.

Implementation without <pre> tag

Despite using writeln() method, both statements are in the same line because HTML ignores the newline command.

Using writeln() without <pre> tag.

Implementation with the <pre> tag

HTML recognizes the newline command. Therefore, the second statement is written in a new line.

Using writeln() with <pre> tag.
Code explanation
  • Line 12: document.writeln(exp1 , exp2) passes two parameters to the function to write in the HTML document. This prints both parameters in the same line and adds a new line after it.

  • Line 13: document.writeln(exp1) passes one parameter to the function to write in the HTML document. This prints the parameter in the same line and adds a new line after it.

Note: Both the lines generate the same output.

Quick concept revision

A quick revision to test your understanding.

Q

What does ln represent?

A)

One tab space

B)

New line

C)

Previous line

Key differences between write() and writeln()

Both write() and writeln() methods belong to the document object and allow you to output content to the HTML document. However, there is a subtle difference between the two:

document.write (content)

document.writeln (content)

The write() method writes the specified content directly to the document. It does not add a newline character at the end.

The writeln() method works similarly to write(), but it adds a newline character after writing the content.

This can cause content to appear on the same line unless additional HTML tags (like <br>) are used.

This makes writeln() more suitable when you want to ensure that each output is separated by a line break.

A quick quiz to test your understanding about write() and writeln().

Question
<pre>
<script>
document.writeln("line 1");
document.write("line 2");
</script>
</pre>

Will the above code snippet write both statements on a separate line?

Show Answer

When to use the writeln() method

While the writeln() method can be helpful in some situations, it is relatively outdated in modern web development practices. Here’s when you might still encounter or consider using it:

  1. Quick prototyping:

    1. writeln() can be useful during quick prototyping or learning phases when you need to output simple content to the document without worrying about styling, layout, or structure.

  2. Debugging:

    1. It can be used for simple debugging purposes, such as printing values or calculation results to the document.

  3. Scripting for educational purposes:

    1. When teaching the fundamentals of JavaScript or DOM manipulation, writeln() offers a quick way to demonstrate content writing.

However, in production-level applications, using the writeln() method is generally discouraged. Modern web development encourages better methods for manipulating the DOM dynamically, such as using innerHTML, textContent, or DOM manipulation APIs.

Modern alternatives to writeln()

In contemporary web development, the writeln() method is not commonly used for several reasons, including its effect on the document structure, its limited control over formatting, and the preference for more flexible and powerful DOM manipulation methods.

Here are some of the modern alternatives:

  1. Using innerHTML:

    1. The innerHTML property allows developers to set or retrieve the HTML content inside an element. It’s more versatile because it can handle HTML markup, not just plain text.

document.getElementById("output").innerHTML = "Hello, World!";
  1. Using textContent:

    1. The textContent property allows you to set or retrieve only the text content of an element, without any HTML formatting. This is useful when you want to ensure that only text is written, not HTML.

document.getElementById("output").textContent = "Hello, World!";
  1. Using createElement() and appendChild():

    1. For more complex DOM manipulation, developers can create elements dynamically and append them to the DOM.

let newParagraph = document.createElement("p");
newParagraph.textContent = "Hello, World!";
document.body.appendChild(newParagraph);

These modern approaches offer better control over the content, structure, and formatting of a web page, making them preferable for dynamic content generation.

Warnings

While the writeln() method can be useful in specific situations, it comes with a few important warnings that developers should be aware of:

  1. Document overwrite:

    1. If writeln() is called after the document has already finished loading, it can overwrite the entire document. This happens because the writeln() method was originally designed to work during the document’s initial load, and calling it after loading causes the page content to be replaced with whatever is written.

  2. No control over formatting:

    1. The writeln() method does not provide fine-grained control over the layout or formatting of the document, unlike modern methods that allow you to style or structure the content in more meaningful ways (using CSS or other DOM manipulation APIs).

  3. Not suitable for dynamic content updates:

    1. For interactive web applications that rely on dynamic content updates (e.g., user input, server responses), writeln() is not suitable. Methods like innerHTML, textContent, and others provide greater flexibility and control.

Browser compatibility

The writeln() method is supported by all modern browsers.

writeln() method supported in all browsers
writeln() method supported in all browsers

Conclusion

The writeln() method in JavaScript is a simple and outdated way to write content to an HTML document, ensuring that each piece of content is followed by a newline. While it served its purpose in earlier web development, modern practices favor more flexible and robust DOM manipulation techniques, such as using innerHTML, textContent, and DOM methods like createElement() and appendChild().

In summary, while writeln() may still be useful for quick testing or educational purposes, it is not recommended for use in production code due to its limitations, potential for clearing the document, and lack of fine-grained control.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is the difference between document.write() and innerHTML?

document.write() writes content directly to the document, while innerHTML sets or retrieves the HTML content inside an element, offering more flexibility and control.


What is document.write() used for?

document.write() is used to write content directly to an HTML document, typically during the initial page load.


Why avoid using document.write()?

It can overwrite the entire document if called after the page has loaded, and it lacks control over content formatting.


What is the danger of using the document.write() method?

Using document.write() after the page load can overwrite existing content, disrupting the web page.


Is document writing safe?

It’s generally unsafe for dynamic content and production use, as it can interfere with the page structure and overwrite content.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved