-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
86 lines (65 loc) · 3 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
---
title: "SyntaxError: string literal contains an unescaped line break"
slug: Web/JavaScript/Reference/Errors/String_literal_EOL
page-type: javascript-error
---
{{jsSidebar("Errors")}}
The JavaScript error "string literal contains an unescaped line break" occurs when there is an unterminated
[string literal](/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#string_literals) somewhere. String literals must be enclosed by single
(`'`) or double (`"`) quotes and cannot split across multiple lines.
## Message
```plain
SyntaxError: Invalid or unexpected token (V8-based)
SyntaxError: '' string literal contains an unescaped line break (Firefox)
SyntaxError: Unexpected EOF (Safari)
```
## Error type
{{jsxref("SyntaxError")}}
## What went wrong?
There is an unterminated
[string literal](/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#string_literals) somewhere. String literals must be
enclosed by single (`'`) or double (`"`) quotes. JavaScript makes
no distinction between single-quoted strings and double-quoted strings.
[Escape sequences](/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#escape_sequences) work
in strings created with either single or double quotes.
To fix this error, check if:
- you have opening and closing quotes (single or double) for your string literal,
- you have escaped your string literal correctly,
- your string literal isn't split across multiple lines.
## Examples
### Multiple lines
You can't split a string across multiple lines like this in JavaScript:
```js-nolint example-bad
const longString = "This is a very long string which needs
to wrap across multiple lines because
otherwise my code is unreadable.";
// SyntaxError: unterminated string literal
```
Instead, use the [+ operator](/en-US/docs/Web/JavaScript/Reference/Operators/Addition),
a backslash, or [template literals](/en-US/docs/Web/JavaScript/Reference/Template_literals).
The `+` operator variant looks like this:
```js example-good
const longString =
"This is a very long string which needs " +
"to wrap across multiple lines because " +
"otherwise my code is unreadable.";
```
Or you can use the backslash character ("\\") at the end of each line to indicate that
the string will continue on the next line. Make sure there is no space or any other
character after the backslash (except for a line break), or as an indent; otherwise it
will not work. That form looks like this:
```js example-good
const longString =
"This is a very long string which needs \
to wrap across multiple lines because \
otherwise my code is unreadable.";
```
Another possibility is to use [template literals](/en-US/docs/Web/JavaScript/Reference/Template_literals).
```js example-good
const longString = `This is a very long string which needs
to wrap across multiple lines because
otherwise my code is unreadable.`;
```
## See also
- [string literal](/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#string_literals)
- [Template literals](/en-US/docs/Web/JavaScript/Reference/Template_literals)