Q:

How to Create Multiline Strings in JavaScript

0

How to Create Multiline Strings in JavaScript

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use String Concatenation

You can use string concatenation (through + operator) to create a multi-line string.

The following example will show you how to compose some HTML content dynamically in JavaScript using the string concatenation and print it on a webpage.

<script>
    // Creating multi-line string
    var str = '<div class="content">' +
                  '<h1>This is a heading</h1>' +
                  '<p>This is a paragraph of text.</p>' +
              '</div>';
    
    // Printing the string
    document.write(str);
</script>

There is even better way to do this. Since ES6 you can use template literals to create multi-line strings very easily. Template literals uses backticks (`) syntax, as shown in the example below:

<script>
    // Creating multi-line string
    var str = `<div class="content">
                  <h1>This is a heading</h1>
                  <p>This is a paragraph of text.</p>
               </div>`;
    
    // Printing the string
    document.write(str);
</script>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Check Whether a String Contains a Substring... >>
<< How to make the first letter of a string uppercase...