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 1061790 of ReferenceError: assignment to undeclared variable "x"

  • Revision slug: Web/JavaScript/Reference/Errors/Undeclared_var
  • Revision title: ReferenceError: assignment to undeclared variable "x"
  • Revision id: 1061790
  • Created:
  • Creator: fscholz
  • Is current revision? Yes
  • Comment

Revision Content

{{jsSidebar("Errors")}}

Message

ReferenceError: assignment to undeclared variable "x" (Firefox)
ReferenceError: "x" is not defined (Chrome)
ReferenceError: Variable undefined in strict mode (Edge)

Error type

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

What went wrong?

A value has been assigned to an undeclared variable. In other words, there was an assignment without the var keyword. There are some differences between declared and undeclared variables, which might lead to unexpected results and that's why JavaScript presents an error in strict mode.

Three things to note about declared and undeclared variables:

  • Declared variables are constrained in the execution context in which they are declared. Undeclared variables are always global.
  • Declared variables are created before any code is executed. Undeclared variables do not exist until the code assigning to them is executed.
  • Declared variables are a non-configurable property of their execution context (function or global). Undeclared variables are configurable (e.g. can be deleted).

For more details and examples, see the var reference page.

Errors about undeclared variable assignments occur in strict mode code only. In non-strict code, they are silently ignored.

Examples

Invalid cases

In this case, the variable "bar" is an undeclared variable.

function foo() { 
  "use strict"; 
  bar = true; 
} 
foo(); // ReferenceError: assignment to undeclared variable bar

Valid cases

To make "bar" a declared variable, you can add the var keyword in front of it.

function foo() {
  "use strict";
  var bar = true;
}
foo();

See also

Revision Source

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

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

<pre class="syntaxbox">
ReferenceError: assignment to undeclared variable "x" (Firefox)
ReferenceError: "x" is not defined (Chrome)
ReferenceError: Variable undefined in strict mode (Edge)
</pre>

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

<p>{{jsxref("ReferenceError")}} 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>A value has been assigned to an undeclared variable. In other words, there was an assignment without the <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code> keyword. There are some differences between declared and undeclared variables, which might lead to unexpected results and that's why JavaScript presents an error in strict mode.</p>

<p>Three things to note about declared and undeclared variables:</p>

<ul>
 <li>Declared variables are constrained in the execution context in which they are declared. Undeclared variables are always global.</li>
 <li>Declared variables are created before any code is executed. Undeclared variables do not exist until the code assigning to them is executed.</li>
 <li>Declared variables are a non-configurable property of their execution context (function or global). Undeclared variables are configurable (e.g. can be deleted).</li>
</ul>

<p>For more details and examples, see the <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code> reference page.</p>

<p>Errors about undeclared variable assignments occur in <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode code</a> only. In non-strict code, they are silently ignored.</p>

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

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

<p>In this case, the variable "bar" is an undeclared variable.</p>

<pre class="brush: js example-bad">
function foo() { 
  "use strict"; 
  bar = true; 
} 
foo(); // ReferenceError: assignment to undeclared variable bar
</pre>

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

<p>To make "bar" a declared variable, you can add the <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code> keyword in front of it.</p>

<pre class="brush: js example-good">
function foo() {
  "use strict";
  var bar = true;
}
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