How to create Multiline string in JavaScript?

featured Image

In this article, we will learn How to create Multilline String in Javascript.

EMCAScript 6 ( ES6 ) has introduce a new type of literal called template literals.

Template literals (Template strings) have many features like variable / expression interpolation, nesting templates, tagged templates etc. Template literals are delimited with backticks (`), and allows embedded expressions called substitutions.

And among with these features, we also got the Multi-line strings, which helps us to create multi-line string in JavaScript.

In Multi-line strings, any newline characters in the code or source is considered as a part of the template literal.

Create Mutli-line strings in JavaScript

When we use normal strings, we would have to use the following syntax to create multi-line strings in our JavaScript code:

console.log('string text line 1n' +
'string text line 2');
//OUTPUT
// "string text line 1
// string text line 2"

In the above code, in order to create a new line we had to use n, it create a new line.

However, now using the template literals, we can write multi-line like this example below:

console.log(`string text line 1
string text line 2`);

//OUTPUT

// "string text line 1
// string text line 2"

We just have to write the strings inside the backticks (`) and it will be displayed as multiline.


Related Topics:

Get the 10 characters from a string using Javascript.

String Interpolation In JavaScript

How to create Multiline string in JavaScript?

Generate random string/characters in JavaScript

How to Remove Last Character from a string in JavaScript

Check if String Is a Number or Not in JavaScript

How can I do String interpolation in JavaScript?

Compare Two Strings in JavaScript | String Comparison

Related Posts

featured Image

Get the 10 characters from a string using JavaScript.

Here, in this article we will learn how to get the first 10 character from any string using JavaScript. Here we will be using JavaScript’s String method substring. What is…

Read more
featured Image

Convert date to long date format using JavaScript

In this article we will look into how to convert a date to a long date format using JavaScript. To convert it to a long format we will be using…

Read more
featured Image

Prevent body from scrolling when a modal is opened

Here, in this article, we will learn how to prevent the body from scrolling when a modal or a pop-up is opened using JavaScript. The scroll event is not cancelable….

Read more