How to Add New Lines in JavaScript?19 Apr 2025 | 4 min read Adding a New Line in JavaScriptAdding new lines in texts is a common issue for many of us for readability or formatting outputs, and in JavaScript it can be done in several ways. So now how do we actually do this, well with multiple techniques and methods such as using escape sequences, ES6 template literals, built-in functions, console log and document write method. Here are a few of the most common ways: Using Escape SequenceJavaScript uses the escape sequence \n to indicate that we want to write a new line in the string. This is inline within a string, and the ends up telling the JavaScript interpreter that it will break the content into a new line. It will work in your alerts and console logs with these methods. Code: Output: ![]() Calling Console log methodCalling console.log() without any argument just adds a new line to the console. It is especially useful to separate different outputs by adding space in between for easy viewing. Code: Output: ![]() Using document.write()The document.write() method is often used to write directly to the HTML document. This approach uses HTML tag to add a new line. Code: Output: ![]() Using JavaScript document.writeln() MethodThe document.writeln() method is known in a similar way as document.write() method and this method also adds an end-of-line character after the command you call, so it could be useful for printing output in line-per-line outputs. Code: Output: ![]() ConclusionIn JavaScript, there are different methods to add new lines based on the situation. Output splitting can be achieved in various ways in JavaScript, using escape sequences, template literals, or any method manipulating the DOM so choose a method that works best for you. Next TopicJavascript If-Else Shorthand |
Vectors are basic mathematical objects defined by their size and direction. In mathematics, a vector is a depiction of a magnitude with both size and direction and is usually displayed as an arrow on a graph. Vectors are commonly utilized in computer science and programming, particularly...
7 min read
What is the background color? Using the background color feature, it is possible to insert a background color for a web page. The background-color property within an HTML element will be responsible for this. This affects the whole size of the element that has been specified, except...
4 min read
What is Hashmap in JavaScript? In JavaScript, Hashmap is an important data structure that helps us to store and retrieve key-value pairs efficiently. With the use of Hashmap, we can provide quick access to values which is based on their keys which are commonly used to optimize...
6 min read
JavaScript this keyword The this keyword is a reference variable that refers to the current object. Here, we will learn about this keyword with help of different examples. Example Let's see a simple example of this keyword. <script> var address= { company:"TpointTech", city:"Noida", state:"UP", fullAddress:function() { return this.company+" "+this.city+" "+this.state; } }; var fetch=address.fullAddress(); document.writeln(fetch); </script> Output: TpointTech Noida UP The following ways can...
1 min read
Arrays are one of the most helpful facilities in JavaScript that are utilized for storing various elements. Sometimes, a typical task is replacing the existing elements with new ones. Either based on specific conditions or simply using the opportunity to modify the elements at selected indices,...
4 min read
The offsetHeight is an HTML DOM property, which is used by JavaScript programming language. It returns the visible height of an element in pixels that includes the height of visible content, border, padding, and scrollbar if present. The offsetHeight is often used with offsetWidth property....
4 min read
JavaScript comes with something other than a well-known or utilized inbuilt method called expand. However, we are likely referring to a particular context or library where an expand method has been implemented in a unique or customized way. What is an Expand Method? A custom function or utility...
4 min read
An amazing JavaScript framework is a set of JavaScript code libraries that provides internet builders with prepared-to-use code for unusual programming obligations. It is a specialized application framework that permits you to increase internet applications in a specific context. JavaScript Framework performs an important function in the development...
4 min read
The double question mark (??) denotes the nullish coalescing operator introduced in ES6. When its left-hand side operand is undefined or null, the nullish coalescing (??) operator returns its right-hand side operand. In all other cases, the logical operator returns its left-hand side operand. The nullish...
5 min read
? 1. The Fundamentals of IIFE 1.1 Overview In JavaScript, a function referred to as an Immediately Invoked Function Expression (IIFE) is known immediately upon definition. An on-the-spot invocation or execution of a feature expression defines an IIFE, as counseled by its call. Regular features, alternatively, need to be...
9 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India