How to Add a Line Break in JavaScript? Last Updated : 27 Jun, 2024 Comments Improve Suggest changes Like Article Like Report JavaScript is the behavioural language renowned for its ability to dynamically alter the functionality of graphical user interfaces (GUI). In web development, JavaScript empowers developers to create interactive and responsive web applications by manipulating the Document Object Model (DOM). In this article, we will explore two different approaches to add a Line Break in JavaScript.These are the following approaches: Table of ContentUsing innerHTMLUsing createTextNode and appendChildUsing innerHTMLIn this approach, we are using the innerHTML property in JavaScript to add a line break (<br>) dynamically to the content of an HTML element with the ID "geeksTitle." This allows us to modify the HTML content directly from JavaScript, making it a straightforward method for adding visual elements like line breaks.Example: The below example uses innerHTML to add a Line Break in JavaScript. HTML <!DOCTYPE html> <html lang="en"> <head> <title>Example 1</title> <style> h1 { color: green; } </style> </head> <body> <h1 id="geeksTitle">GeeksforGeeks</h1> <h3>Approach 1: Using innerHTML</h3> <button onclick="addLineBreak()">Add Line Break</button> <script> function addLineBreak() { const title = document.getElementById('geeksTitle'); title.innerHTML += '<br>'; } </script> </body> </html> Output: Using createTextNode and appendChildIn this approach, we are using JavaScript's createTextNode and appendChild methods to dynamically add a line break (<br>) to the HTML DOM. The addLineBreak function targets the <h3> element with the id "approachTitle" and inserts the line break before its next sibling, which in this case is the button.Example: The below example uses createTextNode and appendChild to add a Line Break in JavaScript. HTML <!DOCTYPE html> <html lang="en"> <head> <title>Example 2</title> <style> h1 { color: green; } </style> </head> <body> <h1 id="geeksTitle">GeeksforGeeks</h1> <h3 id="approachTitle">Approach 2: Using createTextNode and appendChild</h3> <script> function addLineBreak() { const approachTitle = document.getElementById('approachTitle'); const lineBreak = document.createElement('br'); approachTitle.parentNode .insertBefore(lineBreak, approachTitle.nextSibling); } </script> <button onclick="addLineBreak()">Add Line Break</button> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Add a Line Break in JavaScript? G gpancomputer Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to Create a New Line in JavaScript ? 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.JavaScriptconsole.log("GfG"); console.log("\n"); // New line console.log("Co 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 Add Line Break in CSS? CSS provides flexible options to add or manage line breaks in your content. Here are some effective techniques:1. Using the white-space PropertyThe white-space property allows controlling how text wraps and respects line breaks within an element.HTML<html> <head> <style> .break { w 2 min read 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 JavaScript - How to Remove All Line Breaks From a String? Here are different ways to remove all line breakes from a string in JavaScript.1. Using JavaScript RegEx with replace() Method This method uses regular expressions to detect and replace newlines in the string. It is fed into replace function along with a string to replace with, which in our case is 3 min read Like