Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1069514 of SyntaxError: redeclaration of formal parameter "x"

  • Revision slug: Web/JavaScript/Reference/Errors/Redeclared_parameter
  • Revision title: SyntaxError: redeclaration of formal parameter "x"
  • Revision id: 1069514
  • Created:
  • Creator: fscholz
  • Is current revision? Yes
  • Comment new page -- bug 1275240

Revision Content

{{jsSidebar("Errors")}}

Message

SyntaxError: redeclaration of formal parameter "x" (Firefox)
SyntaxError: Identifier "x" has already been declared (Chrome)

Error type

{{jsxref("SyntaxError")}}

What went wrong?

The same variable name occurs as a function parameter and is then redeclared using a let assignment in a function body again. Redeclaring the same variable within the same function or block scope using let is not allowed in JavaScript.

Examples

In this case, the variable "arg" redeclares the argument.

function f(arg) { 
  let arg = "foo"; 
}

// SyntaxError: redeclaration of formal parameter "arg"

If you want to change the value of "arg" in the function body, you can do so, but you do not need to declare the same variable again. In other words: you can omit the let keyword. If you want to create a new variable, you need to rename it as conflicts with the function parameter already.

function f(arg) {
  arg = "foo";
}

function f(arg) { 
  let bar = "foo"; 
}

Compatibility notes

  • Prior to Firefox 49 {{geckoRelease(49)}}, this was thrown as a {{jsxref("TypeError")}} ({{bug(1275240)}}).

See also

Revision Source

<div>{{jsSidebar("Errors")}}</div>

<h2 id="Message">Message</h2>

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

<h2 id="Error_type">Error type</h2>

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

<h2 id="What_went_wrong">What went wrong?</h2>

<p>The same variable name occurs as a function parameter and is then redeclared using a <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code> assignment in a function body again. Redeclaring the same variable within the same function or block scope using <code>let</code> is not allowed in JavaScript.</p>

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

<p>In this case, the variable "arg" redeclares the argument.</p>

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

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

<p>If you want to change the value of "arg" in the function body, you can do so, but you do not need to declare the same variable again. In other words: you can omit the <code>let</code> keyword. If you want to create a new variable, you need to rename it as conflicts with the function parameter already.</p>

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

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

<h2>Compatibility notes</h2>

<ul>
 <li>Prior to Firefox 49 {{geckoRelease(49)}}, this was thrown as a {{jsxref("TypeError")}} ({{bug(1275240)}}).</li>
</ul>

<h2 id="See_also">See also</h2>

<ul>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code></li>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/const">const</a></code></li>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code></li>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Grammar_and_Types#Declarations">Declaring variables</a> in the <a href="/en-US/docs/Web/JavaScript/Guide">JavaScript Guide</a></li>
</ul>
Revert to this revision