document.write()
writes content directly to the document, while innerHTML
sets or retrieves the HTML content inside an element, offering more flexibility and control.
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.
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.
The syntax of writeln()
method is:
<pre><script>document.writeln("exp");</script></pre>
<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.
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 |
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.
<pre>
tagDespite using writeln()
method, both statements are in the same line because HTML ignores the newline command.
<pre>
tagHTML recognizes the newline command. Therefore, the second statement is written in a new line.
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.
A quick revision to test your understanding.
What does ln
represent?
One tab space
New line
Previous line
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 | The |
This can cause content to appear on the same line unless additional HTML tags (like | This makes |
A quick quiz to test your understanding about write()
and writeln()
.
<pre>
<script>
document.writeln("line 1");
document.write("line 2");
</script>
</pre>
Will the above code snippet write both statements on a separate line?
writeln()
methodWhile 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:
Quick prototyping:
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.
Debugging:
It can be used for simple debugging purposes, such as printing values or calculation results to the document.
Scripting for educational purposes:
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.
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:
Using innerHTML
:
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!";
Using textContent
:
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!";
Using createElement()
and appendChild()
:
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.
While the writeln()
method can be useful in specific situations, it comes with a few important warnings that developers should be aware of:
Document overwrite:
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.
No control over formatting:
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).
Not suitable for dynamic content updates:
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.
The writeln()
method is supported by all modern browsers.
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.
Haven’t found what you were looking for? Contact Us
Free Resources