JavaScript Multiline String Example

JavaScript Multiline String Example | With new line characters, concatenation, and template string literals, one may generate a multiline string in JavaScript. The most recent syntax allows you to embed parameters into a string and is known as template string literals. There are three methods to construct multi-line strings with JavaScript:-

1) Introducing fresh line characters (\n).
2) Using String concatenation.
3) String literals in template code.

In this article, we’ll go through how to create a multi-line string using each of these three methods. To help you get started, we’ll go over samples of each strategy.

JavaScript Multiline String – Before ES6 New Lines and Concatenation

To divide a string into many lines prior to ECMAScript 6 (ES6), you had to use newline characters or string concatenation. 

let string = "\nhello \nworld"
console.log(string);

Output:-


hello
world

There are new line characters in the string. These symbols are often used in many computer languages. The character ‘\n’ stands for a new line. This character can be used to stretch a string across multiple lines.

Now, let us have a look at the code by using the concatenation operator.

let string = "The basic needs of humans are:" + 
              "\n" + "Water" + "\n" + "Air" + "\n" 
              + "Shelter" + "\n" + "Food" + "\n";
console.log(string); 

Output:-

The basic needs of humans are:
Water
Air
Shelter
Food

We also rely on the new line characters in this scenario. The \n example indicates them. If you want distinct strings to appear on various lines and you have several strings, this method is helpful. But your code could very easily become disorganized. No one enjoys clumsy code.

These are legitimate techniques for writing several lines in JavaScript. But with the introduction of ES6, a new method to accomplish the same goal has emerged: template string literals.

JavaScript Multiline String – Template String Literals

The new line characters are also used in this example. The /n example serves as a sign for these. When you want different strings to appear on different lines, this method is helpful. However, your code may quickly become disorganized. Messy code is not popular.

These approaches to creating multiple lines in JavaScript are acceptable. But template string literals, a new feature of ES6, offer a new way to accomplish the same task.

let string = `
  Hello
  World 
  This 
  Is
  Know 
  Program
`

console.log(string);

Output:-

Hello
World
This
Is
Know
Program

A string that covers many lines has been declared. A string literal is indicated by the backtick symbol.

A multiline string can be created with JavaScript in three different methods. One can make use of template literals, the concatenation operator, and the new line character (n). In ES6, template literals were added. They also enable you to add a variable’s contents to a line. 

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *