Thursday, September 4, 2025
HomeLanguagesJavascriptHow to write multi-line strings in template literals ?

How to write multi-line strings in template literals ?

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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6746 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS