![]() |
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`The \n escape sequence represents a newline character in JavaScript strings and it can used to render a new line in JavaScript. Example: The below code uses the escape sequence `\n` to add a new line in JavaScript. Javascript
Output
GFG GeeksforGeeks is a computer science portal for geeks. Using ECMAScript6 (ES6) template literalsIn ECMAScript6, the backticks (“) are used to create a template literal. Template literals can span multiple lines without the need for escape characters, making it easier to write and read multiline strings in JavaScript. Example: The below code uses the ES6 template literals to add a new line in JavaScript. Javascript
Output
Q: What is GeeksforGeeks? A: GeeksforGeeks is a computer science portal for geeks. Using JavaScript console.log() MethodThe console.log() method is a part of the JavaScript Console API, which provides methods for interacting with the browser’s console. It also returns a new line when the method is called and utilized so this approach can be used in place of an escape character. Example: The below code uses the console.log() method to add a new line in JavaScript. Javascript
Output
GFG GeeksforGeeks Using JavaScript document.write() MethodThe document.write() method is a part of the JavaScript Document Object Model (DOM) API. It is used to dynamically write content to a document, specifically the HTML document. This method is especially useful if a new line has to be added or rendered on an HTML page using the <br> tag. Example: The below code uses the document.write() method to add a new line in an HTML document using JavaScript. Javascript
HTML
Output: Using JavaScript document.writeln() MethodThis 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
Output: |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |