How to use document.writeln() Method In Javascript
This method is especially useful if a new line has to be added to an HTML page. The only difference between this method and document.write() is that the document.writeln() automatically adds a new line after the rendered content instead of adding the <br> tag as done in the previous approach.
Example: The below code uses the document.writeln() method to add a new line in an HTML document using JavaScript.
HTML
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < title >HTML Document</ title > </ head > < body > < h1 style = "color:green;" > w3wiki </ h1 > <!-- JavaScript --> < script > document.writeln ("A computer science"); document.writeln ("portal for geeks."); </ script > </ body > </ html > |
Output:
How to Create a New Line in JavaScript ?
In JavaScript, a new line is typically represented by a specific escape sequence or character within a string. This is commonly used in various programming languages to control the formatting of text and create multiline strings. A new line can be made in JavaScript using the below-discussed methods.
Table of Content
- Using Escape Sequence `\n`
- Using ECMAScript6 (ES6) template literals
- Using JavaScript console.log() Method
- Using JavaScript document.write() Method
- Using JavaScript document.writeln() Method
Contact Us