Open In App

How to write multi-line strings in template literals ?

Last Updated : 17 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Template literals are introduced in ES6 and by this, we use strings in a modern way. Normally for defining string, we use double/single quotes ( ” ” or ‘ ‘ ) in JavaScript. But in template literals, we use backtick ( ` ` ).

Let us see how to write multiline strings in template literals.

Example 1: We write multiline string by template literals.

Javascript




const multilineString = `How
are you
doing
I am fine`;
 
console.log(multilineString);


Output:

How      
are you  
doing    
I am fine

Example 2: If you use double/single quote to write multiline string then we use the newline character (\n).  Use an extra backslash ( \ ) after every newline character (\n), this backslash tells the JavaScript engine that the string is not over yet.

Javascript




var multilineString =
"How \n\
are you \n\
doing \n\
I am fine";
 
console.log(multilineString);


Output:

How 
are you
doing
I am fine

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads