This post will discuss how to create multiline strings in JavaScript.

1. Using String Concatenation

You can use the concatenation operator + to show the string on multiple lines. This can be done using either double or single quotes.

Download  Run Code

2. Backslash in string

Instead of concatenating multiple strings, you can use the backslash (\) escape character to escape the newline.

Download  Run Code

3. Using template literals

Template literals is a recent addition to JavaScript which allows multiline strings and support features like string interpolation. Template literals are enclosed by the backtick character. The string, as well as newlines in the source, will be preserved.

This makes the code more readable and eliminates the need for concatenation or escaping. The following program demonstrates how to create multiline strings using the template literals.

Download  Run Code

That’s all about creating multi-line strings in JavaScript.