aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/errors/redeclared_parameter/index.html
blob: c8baee62ea5150089a9061e89d7ba4cfb0606951 (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
---
title: 'SyntaxError: redeclaration of formal parameter "x"'
slug: Web/JavaScript/Reference/Errors/Redeclared_parameter
tags:
  - Error
  - Errors
  - JavaScript
  - SyntaxError
translation_of: Web/JavaScript/Reference/Errors/Redeclared_parameter
---
<div>{{jsSidebar("Errors")}}</div>

<p>JavaScript の例外 "redeclaration of formal parameter" は、関数の引数名としてある変数名が発生した後、関数本体内の {{jsxref("Statements/let", "let")}} 代入文を使用して同じ変数名を再度宣言すると発生します。</p>

<h2 id="Message" name="Message">メッセージ</h2>

<pre class="syntaxbox notranslate">SyntaxError: Let/Const redeclaration (Edge)
SyntaxError: redeclaration of formal parameter "x" (Firefox)
SyntaxError: Identifier "x" has already been declared (Chrome)
</pre>

<h2 id="Error_type" name="Error_type">エラー種別</h2>

<p>{{jsxref("SyntaxError")}}</p>

<h2 id="What_went_wrong" name="What_went_wrong">エラーの原因</h2>

<p>関数の引数と同名の変数があり、関数内で再度 {{jsxref("Statements/let", "let")}} 代入文を使用して再宣言しています。 JavaScript では、同じ関数内やブロックスコープ内で <code>let</code> を使用して同じ変数を再宣言できません。</p>

<h2 id="Examples" name="Examples"></h2>

<h3 id="Redeclared_argument" name="Redeclared_argument">再宣言した引数</h3>

<p>この場合、変数 "arg" は、引数を再宣言しています。</p>

<pre class="brush: js example-bad notranslate">function f(arg) {
  let arg = 'foo';
}

// SyntaxError: redeclaration of formal parameter "arg"
</pre>

<p>関数の本体内で "arg" の値を変更したい場合、これを行うことはできますが、再び同じ変数を宣言する必要はありません。言い換えれば、 <code>let</code> キーワードを省略することができます。新しい変数を生成する場合は、既存の関数の引数と競合するので、変数名を変更する必要があります。</p>

<pre class="brush: js example-good notranslate">function f(arg) {
  arg = 'foo';
}

function f(arg) {
  let bar = 'foo';
}
</pre>

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li>{{jsxref("Statements/let", "let")}}</li>
 <li>{{jsxref("Statements/const", "const")}}</li>
 <li>{{jsxref("Statements/var", "var")}}</li>
 <li><a href="/ja/docs/Web/JavaScript/Guide">JavaScript ガイド</a> 内の <a href="/ja/docs/Web/JavaScript/Guide/Grammar_and_Types#Declarations">宣言</a></li>
</ul>