Creating a multiline string in the past involved effort and resulted in a lot of noise in code. Template literals may be single line or multiline—there is no special syntax to set the two apart, except for line breaks.
Here’s a multiline string with expressions in it.
| const name = 'John Doe'; |
| |
| const message = `Dear ${name}, |
| We're delighted to let you know that you have been included in |
| our routine spam starting with this one. |
| |
| You can thank us later. |
| `; |
| |
| console.log(message); |
Start the string with a backtick, continue each new line with a line break, and end the string with the ending backtick. Expressions in the string are optional.
The output of this code is
| Dear John Doe, |
| We're delighted to let you know that you have been included in |
| our routine spam starting with this one. |
| |
| You can thank us later. |
Multiline strings preserve indentations, and that can be inconvenient if we have the template literals in an indented region, like within a function or an if condition. We’ll look at a possible workaround for that issue in the exercises at the end of this chapter.