June 2018
Beginner
288 pages
6h 31m
English
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, ... |
Read now
Unlock full access