How to create Multiline string in JavaScript?

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

Scroll to Top