How to assign a multiline string to a variable in JavaScript?
Hello everyone! I'm new to JavaScript and ran into a problem. I need to store a long text that spans multiple lines in a variable. For example, I want to do something like this:
```javascript
let myString = "This is the first line\nThis is the second line\nAnd the third line";
```
But when I write such code, JavaScript throws an error: "Uncaught SyntaxError: unterminated string literal". I understand that quotes must be closed on the same line, but how do I handle multiline text then?
I tried using a backslash at the end of each line:
```javascript
let myString = "This is the first line \
This is the second line \
And the third line";
```
This seems to work, but the text is output as a single line, not three. I need the lines to be preserved as separate lines with line breaks.
No answers yet.
Log in to leave an answer
Blogs