JavaScript

How to Create Multiline Strings in JavaScript

Multiline strings were not supported in JavaScript 2015, but when ES6 came out and introduced string literals. ES6 supports multiline strings. There are different ways to handle multiline strings.

We create Multiline strings using pattern literals. Strings are delimited with a backquote or grave accent ``, as opposed to the single/double quote delimiter, you can use the + operator.
 

Method 1: using single/double quote delimiter with the + operator
// Creating a multiline string
var str = '<div class="row">' +
               '<h1>Title</h1>' +
               '<p> This is a paragraph.</p>' +
           '</div>';
    
// Display the value of str
document.write(str);

 

 

Method 2: using backquote or grave accent ``

When using backquote or grave accent ``, you don’t need to use the + operator.

// Creating a multiline string
var str = `<div class="row"> 
               <h1>Title</h1> 
               <p> This is a paragraph.</p> 
           </div>`;
    
// Display the value of str
document.write(str);
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *