blob: ab19a0f4a8b82e960fdf49a8b94a8121ce581838 (
plain)
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
|
---
title: 'SyntaxError: unterminated string literal'
slug: Web/JavaScript/Reference/Errors/Unterminated_string_literal
tags:
- Error
- Errors
- JavaScript
- SyntaxError
translation_of: Web/JavaScript/Reference/Errors/Unterminated_string_literal
---
<div>{{jsSidebar("Errors")}}</div>
<p>JavaScript のエラー "unterminated string literal" は、どこかに終了していない<a href="/ja/docs/Web/JavaScript/Guide/Grammar_and_types#string_literals">文字列リテラル</a>があった場合に発生します。文字列リテラルは単一引用符 (<code>'</code>) または二重引用符 (<code>"</code>) で囲む必要があります。</p>
<h2 id="Message">エラーメッセージ</h2>
<pre class="brush: js">SyntaxError: Unterminated string constant (Edge)
SyntaxError: unterminated string literal (Firefox)
</pre>
<h2 id="Error_type">エラーの種類</h2>
<p>{{jsxref("SyntaxError")}}</p>
<h2 id="What_went_wrong">エラーの原因</h2>
<p>どこかに終端されていない<a href="/ja/docs/Web/JavaScript/Guide/Grammar_and_types#string_literals">文字列リテラル</a>があります。文字列リテラルは単一引用符 (<code>'</code>) または二重引用符 (<code>"</code>) で囲む必要があります。JavaScript は、単一引用符の文字列と二重引用符の文字列を区別しません。<a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/String#escape_notation">エスケープシーケンス</a> は単一引用符と二重引用符、どちらの文字列でも動作します。このエラーを修正するためには、次の点をチェックしてください。</p>
<ul>
<li>文字列リテラルのために、単一引用符または二重引用符の開始と終了を行っている。</li>
<li>文字列リテラルを正しくエスケープしている。</li>
<li>文字列リテラルが複数行に分けられていない。</li>
</ul>
<h2 id="Examples">例</h2>
<h3 id="Multiple_lines">複数行</h3>
<p>JavaScript では、次のように複数行にまたがる文字列を分割できません。</p>
<pre class="brush: js example-bad">var longString = 'This is a very long string which needs
to wrap across multiple lines because
otherwise my code is unreadable.';
// SyntaxError: unterminated string literal</pre>
<p>代わりに、<a href="/ja/docs/Web/JavaScript/Reference/Operators/Addition">+ 演算子</a> かバックスラッシュ、<a href="/ja/docs/Web/JavaScript/Reference/Template_literals">テンプレートリテラル</a> を使用します。<code>+</code> 演算子の場合、次のようになります。</p>
<pre class="brush: js example-good">var longString = 'This is a very long string which needs ' +
'to wrap across multiple lines because ' +
'otherwise my code is unreadable.';
</pre>
<p>または、文字列が次のように続くことを示すために、各行の終わりにバックスラッシュ文字("\")を使用することもできます。バックスラッシュの後に、 (改行を除いて) スペースや文字、インデントを入れないようにしてください。そうしないと動作しません。バックスラッシュの場合、次のようになります。</p>
<pre class="brush: js example-good">var longString = 'This is a very long string which needs \
to wrap across multiple lines because \
otherwise my code is unreadable.';
</pre>
<p>ECMAScript 2015 環境でサポートされている<a href="/ja/docs/Web/JavaScript/Reference/Template_literals">テンプレートリテラル</a>を使っても改行可能です。</p>
<pre class="brush: js example-good">var longString = `This is a very long string which needs
to wrap across multiple lines because
otherwise my code is unreadable.`;</pre>
<h2 id="See_also">関連情報</h2>
<ul>
<li><a href="/ja/docs/Web/JavaScript/Guide/Grammar_and_types#string_literals">文字列リテラル</a></li>
<li><a href="/ja/docs/Web/JavaScript/Reference/Template_literals">テンプレートリテラル</a></li>
</ul>
|