Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to write multi-line strings in template literals ?

  • Difficulty Level : Easy
  • Last Updated : 03 Dec, 2021

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.

HTML




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

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.

HTML




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

Output:

How 
are you
doing
I am fine

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!