How to Create a New Line in JavaScript ?
Last Updated :
09 Nov, 2024
A new line can be made in JavaScript using the below-discussed methods.
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.
JavaScript
console.log("GfG");
console.log("\n"); // New line
console.log("Courses\n"); // New Line With Text
console.log("Learn");
console.log("\n\n"); // Two New Lines
console.log("Grow");
Using ECMAScript6 (ES6) template literals
In 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
console.log(`Q: What is GfG?
A: GeeksforGeeks`);
Using Empty Call of console.log()
The 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
console.log('GFG');
console.log();
console.log('GeeksforGeeks');
In document.write()
The 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.
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;">GeeksforGeeks</h1>
<script src="./script.js"></script>
</body>
</html>
JavaScript
document.write ("A computer science portal");
document.write ("<br>");
document.write ("for geeks.");
Output:

Using JavaScript document.writeln() Method
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;">
GeeksforGeeks
</h1>
<!-- JavaScript -->
<script>
document.writeln
("A computer science");
document.writeln
("portal for geeks.");
</script>
</body>
</html>
Output:

Similar Reads
How to create multi-line strings in JavaScript ? In JavaScript, the absence of native support for multi-line strings was a longstanding challenge. However, with the advent of ES6 (ECMAScript 2015), the things changed dramatically. ES6 introduced string literals, offering robust support for multi-line strings, which was a significant enhancement fo
3 min read
How to create a New Line in PHP ? When working with text output in PHP, like creating a webpage, sending an email, or saving to a file, it's important to know how to add a new line. Adding a new line helps break up the text, makes it easier to read, and keeps it looking right whether it's shown in a browser, a terminal, or a file.Ho
2 min read
How to break JavaScript Code into several lines ? In this article, we will discuss how to break javascript code into several lines. We generally break the code into several lines so that it can be read easily by a third person or the programmer itself. There are a few ways to break JavaScript code into several lines: We can use the backslash \ char
1 min read
How to Create a String with Variables in JavaScript? To create a string with variables in JavaScript, you can use several methods, including string concatenation, template literals, and the String methods. Here are the common approaches:1. Template Literals (Recommended)Template literals are the modern and most preferred way to create strings with var
2 min read
How to Print a String in JavaScript ? In JavaScript, printing a string means displaying the contents of a string variable or literal to the console, webpage, or any other output medium. Strings are a fundamental data type in JavaScript, representing sequences of characters enclosed within single ('') or double ("") quotes. Table of Cont
2 min read