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 1061782 of TypeError: variable "x" redeclares argument

  • Revision slug: Web/JavaScript/Reference/Errors/Var_hides_argument
  • Revision title: TypeError: variable "x" redeclares argument
  • Revision id: 1061782
  • Created:
  • Creator: fscholz
  • Is current revision? Yes
  • Comment

Revision Content

{{jsSidebar("Errors")}}

Message

TypeError: variable "x" redeclares argument (Firefox)

Error type

{{jsxref("TypeError")}} warning in strict mode only.

What went wrong?

The same variable name occurs as a function parameter and is then redeclared using a var assignment in a function body again. This might be a naming conflict and thus JavaScript warns about it.

This error occurs as a warning in strict mode code only. In non-strict code, the redeclaration is silently ignored.

Examples

Invalid cases

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

"use strict";

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

Valid cases

To fix this warning, the var statement can just be omitted, because the variable exists already. In other cases, you might to rename either the function parameter or the variable name.

"use strict";

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

See also

Revision Source

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

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

<pre class="syntaxbox">
TypeError: variable "x" redeclares argument (Firefox)
</pre>

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

<p>{{jsxref("TypeError")}} warning in <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode</a> only.</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/var">var</a></code> assignment in a function body again. This might be a naming conflict and thus JavaScript warns about it.</p>

<p>This error occurs as a warning in <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode code</a> only. In non-strict code, the redeclaration is silently ignored.</p>

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

<h3 id="Invalid_cases">Invalid cases</h3>

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

<pre class="brush: js example-bad">
"use strict";

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

<h3 id="Valid_cases">Valid cases</h3>

<p>To fix this warning, the <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code> statement can just be omitted, because the variable exists already. In other cases, you might to rename either the function parameter or the variable name.</p>

<pre class="brush: js example-good">
"use strict";

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

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

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">Strict mode</a></li>
</ul>
Revert to this revision