Difference between document.write() and document.writeln()

Difference between document.write() and document.writeln()

document.write() and document.writeln() are both methods used to write content to the HTML document. Here's the key difference:

  • document.write(): Writes text to the HTML document. It doesn't append a new line after the text.

document.write("Hello, World!");        

  • document.writeln(): Writes text to the HTML document, and appends a newline character after the text, making it go to the next line.

document.write("Hello, World!");        

In essence, document.writeln() is the same as document.write() but adds a newline at the end. Both are rarely used in modern JavaScript because they overwrite the entire document if called after the document has finished loading. For dynamic content, it's better to use other methods like manipulating the DOM directly.

要查看或添加评论,请登录

Aabhas Saxena的更多文章

社区洞察

其他会员也浏览了